Skip to content

Commit e5d1780

Browse files
authored
feat: no_std Compatibility (#30)
This library is now no_std compatible, it needs `alloc` though.
1 parent 10bdbe3 commit e5d1780

File tree

8 files changed

+51
-8
lines changed

8 files changed

+51
-8
lines changed

.cargo/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[unstable]
2+
features = ["host_dep"]

.github/workflows/build.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ jobs:
4040
with:
4141
command: test
4242

43+
ensure_no_std:
44+
name: Ensure no_std
45+
runs-on: ubuntu-latest
46+
steps:
47+
- uses: actions/checkout@v1
48+
- uses: actions-rs/toolchain@v1
49+
with:
50+
profile: minimal
51+
toolchain: nightly
52+
override: true
53+
- name: Install thumbv6m-none-eabi
54+
run: rustup target add thumbv6m-none-eabi
55+
- name: Build thumbv6m-none-eabi
56+
run: cargo build --target thumbv6m-none-eabi --no-default-features
57+
4358
coverage:
4459
name: Code Coverage
4560
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ readme = "README.md"
88
description = "multibase in rust"
99
homepage = "https://github.com/multiformats/rust-multibase"
1010
repository = "https://github.com/multiformats/rust-multibase"
11-
keywords = ["ipld", "ipfs", "multihash", "multibase", "cid"]
11+
keywords = ["ipld", "ipfs", "multihash", "multibase", "cid", "no_std"]
12+
13+
[features]
14+
default = ["std"]
15+
std = ["data-encoding/std"]
1216

1317
[dependencies]
14-
base-x = "0.2"
15-
data-encoding = "2.2"
16-
data-encoding-macro = "0.1.8"
18+
base-x = { version = "0.2.7", default-features = false }
19+
data-encoding = { version = "2.3.1", default-features = false, features = ["alloc"] }
20+
data-encoding-macro = "0.1.9"
1721

1822
[dev-dependencies]
1923
criterion = "0.3"

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,17 @@ First add this to your `Cargo.toml`
2828

2929
```toml
3030
[dependencies]
31-
multibase = "0.8"
31+
multibase = "0.9"
3232
```
3333

34+
For `no_std`
35+
```
36+
[dependencies]
37+
multibase = { version ="0.9", default-features = false }
38+
```
39+
40+
**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.
41+
3442
Then run `cargo build`.
3543

3644
## Usage

src/base.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::error::{Error, Result};
22
use crate::impls::*;
33

4+
#[cfg(not(feature = "std"))]
5+
use alloc::{string::String, vec::Vec};
6+
47
macro_rules! build_base_enum {
58
( $(#[$attr:meta] $code:expr => $base:ident,)* ) => {
69
/// List of types currently supported in the multibase spec.

src/error.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::{error, fmt};
1+
use core::fmt;
22

33
/// Type alias to use this library's [`Error`] type in a `Result`.
4-
pub type Result<T> = std::result::Result<T, Error>;
4+
pub type Result<T> = core::result::Result<T, Error>;
55

66
/// Error types
77
#[derive(PartialEq, Eq, Clone, Debug)]
@@ -21,7 +21,8 @@ impl fmt::Display for Error {
2121
}
2222
}
2323

24-
impl error::Error for Error {}
24+
#[cfg(feature = "std")]
25+
impl std::error::Error for Error {}
2526

2627
impl From<base_x::DecodeError> for Error {
2728
fn from(_: base_x::DecodeError) -> Self {

src/impls.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use crate::encoding;
22
use crate::error::Result;
33

4+
#[cfg(not(feature = "std"))]
5+
use alloc::{string::String, vec::Vec};
6+
47
macro_rules! derive_base_encoding {
58
( $(#[$doc:meta] $type:ident, $encoding:expr;)* ) => {
69
$(

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
//! Implementation of [multibase](https://github.com/multiformats/multibase) in Rust.
44
55
#![deny(missing_docs)]
6+
#![cfg_attr(not(feature = "std"), no_std)]
7+
8+
#[cfg(not(feature = "std"))]
9+
extern crate alloc;
10+
11+
#[cfg(not(feature = "std"))]
12+
use alloc::{string::String, vec::Vec};
613

714
mod base;
815
mod encoding;

0 commit comments

Comments
 (0)