From 62732d7b7f184d182b5d26c13a6752f1c328f5e1 Mon Sep 17 00:00:00 2001 From: Naoki Ikeguchi Date: Sat, 24 Sep 2022 13:28:13 +0900 Subject: [PATCH 1/2] feat: Support no_std environment --- apdu-core/Cargo.toml | 4 ++++ apdu-core/src/command.rs | 3 +++ apdu-core/src/error.rs | 5 +++-- apdu-core/src/lib.rs | 4 ++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/apdu-core/Cargo.toml b/apdu-core/Cargo.toml index 60342fb..e975cf3 100644 --- a/apdu-core/Cargo.toml +++ b/apdu-core/Cargo.toml @@ -14,3 +14,7 @@ authors = [ ] [dependencies] + +[features] +std = [] +default = ["std"] diff --git a/apdu-core/src/command.rs b/apdu-core/src/command.rs index 11790c5..d0c928b 100644 --- a/apdu-core/src/command.rs +++ b/apdu-core/src/command.rs @@ -1,3 +1,6 @@ +use alloc::vec; +use alloc::vec::Vec; + /// An APDU command to be transmitted pub struct Command { pub cla: u8, diff --git a/apdu-core/src/error.rs b/apdu-core/src/error.rs index e6f57f8..392cf1c 100644 --- a/apdu-core/src/error.rs +++ b/apdu-core/src/error.rs @@ -1,4 +1,4 @@ -use std::fmt::{Display, Formatter}; +use core::fmt::{Display, Formatter}; use crate::Response; @@ -9,7 +9,7 @@ pub struct Error { } impl Display for Error { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { let (sw1, sw2) = self.response.trailer; write!( @@ -20,4 +20,5 @@ impl Display for Error { } } +#[cfg(feature = "std")] impl std::error::Error for Error {} diff --git a/apdu-core/src/lib.rs b/apdu-core/src/lib.rs index 3d6e6d8..38e755a 100644 --- a/apdu-core/src/lib.rs +++ b/apdu-core/src/lib.rs @@ -1,5 +1,9 @@ //! Core library for composing APDU commands and parsing their responses. +#![cfg_attr(not(feature = "std"), no_std)] + +extern crate alloc; + mod command; mod error; mod response; From 343a9a42075e459c8c9596705021e24562ab9d0c Mon Sep 17 00:00:00 2001 From: Naoki Ikeguchi Date: Sat, 24 Sep 2022 13:31:05 +0900 Subject: [PATCH 2/2] docs: Document about no_std support --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 8bde299..0e53f39 100644 --- a/README.md +++ b/README.md @@ -25,5 +25,14 @@ Add to your Cargo.toml as a dependency as follows: apdu = "0.2" ``` +## 🛠 no_std support +apdu-core crate does support no_std environments. +If you are using this crate in no_std, turn `std` feature off by disabling default features: + +```toml +[dependencies] +apdu-core = { version = "0.2", default-features = false } +``` + ## 📄 Documentation See [docs.rs](https://docs.rs/apdu/).