diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..fa2f1bf --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[unstable] +features = ["host_dep"] diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 314115c..a8caaa6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,6 +40,21 @@ jobs: with: command: test + ensure_no_std: + name: Ensure no_std + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: nightly + override: true + - name: Install thumbv6m-none-eabi + run: rustup target add thumbv6m-none-eabi + - name: Build thumbv6m-none-eabi + run: cargo build --target thumbv6m-none-eabi --no-default-features + coverage: name: Code Coverage runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index 9f4c2bc..c7d1c66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,12 +8,16 @@ readme = "README.md" description = "multibase in rust" homepage = "https://github.com/multiformats/rust-multibase" repository = "https://github.com/multiformats/rust-multibase" -keywords = ["ipld", "ipfs", "multihash", "multibase", "cid"] +keywords = ["ipld", "ipfs", "multihash", "multibase", "cid", "no_std"] + +[features] +default = ["std"] +std = ["data-encoding/std"] [dependencies] -base-x = "0.2" -data-encoding = "2.2" -data-encoding-macro = "0.1.8" +base-x = { version = "0.2.7", default-features = false } +data-encoding = { version = "2.3.1", default-features = false, features = ["alloc"] } +data-encoding-macro = "0.1.9" [dev-dependencies] criterion = "0.3" diff --git a/README.md b/README.md index a6b5748..0c3aac7 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,17 @@ First add this to your `Cargo.toml` ```toml [dependencies] -multibase = "0.8" +multibase = "0.9" ``` +For `no_std` +``` +[dependencies] +multibase = { version ="0.9", default-features = false } +``` + +**note**: This crate relies on the [currently unstable](https://github.com/rust-lang/cargo/issues/7915) `host_dep` feature to [compile proc macros with the proper dependencies](https://docs.rs/data-encoding-macro/0.1.10/data_encoding_macro/), thus **requiring nightly rustc** to use. + Then run `cargo build`. ## Usage diff --git a/src/base.rs b/src/base.rs index c783c8c..c6a734b 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1,6 +1,9 @@ use crate::error::{Error, Result}; use crate::impls::*; +#[cfg(not(feature = "std"))] +use alloc::{string::String, vec::Vec}; + macro_rules! build_base_enum { ( $(#[$attr:meta] $code:expr => $base:ident,)* ) => { /// List of types currently supported in the multibase spec. diff --git a/src/error.rs b/src/error.rs index a108578..0c18c1b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,7 @@ -use std::{error, fmt}; +use core::fmt; /// Type alias to use this library's [`Error`] type in a `Result`. -pub type Result = std::result::Result; +pub type Result = core::result::Result; /// Error types #[derive(PartialEq, Eq, Clone, Debug)] @@ -21,7 +21,8 @@ impl fmt::Display for Error { } } -impl error::Error for Error {} +#[cfg(feature = "std")] +impl std::error::Error for Error {} impl From for Error { fn from(_: base_x::DecodeError) -> Self { diff --git a/src/impls.rs b/src/impls.rs index 9a42624..2d55652 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -1,6 +1,9 @@ use crate::encoding; use crate::error::Result; +#[cfg(not(feature = "std"))] +use alloc::{string::String, vec::Vec}; + macro_rules! derive_base_encoding { ( $(#[$doc:meta] $type:ident, $encoding:expr;)* ) => { $( diff --git a/src/lib.rs b/src/lib.rs index 01ca939..3b7960d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,13 @@ //! Implementation of [multibase](https://github.com/multiformats/multibase) in Rust. #![deny(missing_docs)] +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(not(feature = "std"))] +extern crate alloc; + +#[cfg(not(feature = "std"))] +use alloc::{string::String, vec::Vec}; mod base; mod encoding;