Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no_std Compatibility #30

Merged
merged 18 commits into from
Nov 6, 2020
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[unstable]
features = ["host_dep"]
mriise marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/base.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
7 changes: 4 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::result::Result<T, Error>;
pub type Result<T> = core::result::Result<T, Error>;

/// Error types
#[derive(PartialEq, Eq, Clone, Debug)]
Expand All @@ -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<base_x::DecodeError> for Error {
fn from(_: base_x::DecodeError) -> Self {
Expand Down
3 changes: 3 additions & 0 deletions src/impls.rs
Original file line number Diff line number Diff line change
@@ -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;)* ) => {
$(
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down