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

WIP: feedback needed - no_std #25

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ repository = "https://github.com/multiformats/rust-multibase"
keywords = ["ipld", "ipfs", "multihash", "multibase", "cid"]

[dependencies]
base-x = "0.2"
data-encoding = "2.2"
data-encoding-macro = "0.1.8"
base-x = { git = "https://github.com/whalelephant/base-x-rs", branch = "no_std", default-features = false }
data-encoding = { version = "2.3", default-features = false, features = ["alloc"] }
lazy_static = { features = ["spin_no_std"], version = "1.4.0" }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't use spin_no_std with this package's std feature. Spin locks are almost always bad when there is another scheduler.


[dev-dependencies]
criterion = "0.3"
Expand All @@ -25,3 +25,7 @@ harness = false

[workspace]
members = ["cli"]

[features]
default = ["std"]
std = []
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
94 changes: 55 additions & 39 deletions src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
use data_encoding::Encoding;
use data_encoding_macro::{internal_new_encoding, new_encoding};

// Base2 (alphabet: 01)
pub const BASE2: Encoding = new_encoding! {
symbols: "01",
};

// Base8 (alphabet: 01234567)
pub const BASE8: Encoding = new_encoding! {
symbols: "01234567",
};
use data_encoding::{Encoding, Specification};
use lazy_static::lazy_static;

lazy_static! {
// Base2 (alphabet: 01)
pub static ref BASE2: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str("01");
spec.encoding().unwrap()
};

// Base8 (alphabet: 01234567)
pub static ref BASE8: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str("01234567");
spec.encoding().unwrap()
};

// Base32, rfc4648 no padding (alphabet: abcdefghijklmnopqrstuvwxyz234567).
pub static ref BASE32_NOPAD_LOWER: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str("abcdefghijklmnopqrstuvwxyz234567");
spec.encoding().unwrap()
};

// Base32, rfc4648 with padding (alphabet: abcdefghijklmnopqrstuvwxyz234567).
pub static ref BASE32_PAD_LOWER: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str("abcdefghijklmnopqrstuvwxyz234567");
spec.padding = Some('=');
spec.encoding().unwrap()
};

// Base32hex, rfc4648 no padding (alphabet: 0123456789abcdefghijklmnopqrstuv).
pub static ref BASE32HEX_NOPAD_LOWER: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str("0123456789abcdefghijklmnopqrstuv");
spec.encoding().unwrap()
};

// Base32hex, rfc4648 with padding (alphabet: 0123456789abcdefghijklmnopqrstuv).
pub static ref BASE32HEX_PAD_LOWER: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str("0123456789abcdefghijklmnopqrstuv");
spec.padding = Some('=');
spec.encoding().unwrap()
};

// z-base-32 (used by Tahoe-LAFS) (alphabet: ybndrfg8ejkmcpqxot1uwisza345h769).
pub static ref BASE32Z: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str("ybndrfg8ejkmcpqxot1uwisza345h769");
spec.encoding().unwrap()
};
}

/// Base10 (alphabet: 0123456789)
pub const BASE10: &str = "0123456789";
Expand All @@ -20,45 +63,18 @@ pub const BASE16_LOWER: Encoding = data_encoding::HEXLOWER;
// Base16 upper hexadecimal (alphabet: 0123456789ABCDEF).
pub const BASE16_UPPER: Encoding = data_encoding::HEXUPPER;

// Base32, rfc4648 no padding (alphabet: abcdefghijklmnopqrstuvwxyz234567).
pub const BASE32_NOPAD_LOWER: Encoding = new_encoding! {
symbols: "abcdefghijklmnopqrstuvwxyz234567",
};

// Base32, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567).
pub const BASE32_NOPAD_UPPER: Encoding = data_encoding::BASE32_NOPAD;

// Base32, rfc4648 with padding (alphabet: abcdefghijklmnopqrstuvwxyz234567).
pub const BASE32_PAD_LOWER: Encoding = new_encoding! {
symbols: "abcdefghijklmnopqrstuvwxyz234567",
padding: '=',
};

// Base32, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567).
pub const BASE32_PAD_UPPER: Encoding = data_encoding::BASE32;

// Base32hex, rfc4648 no padding (alphabet: 0123456789abcdefghijklmnopqrstuv).
pub const BASE32HEX_NOPAD_LOWER: Encoding = new_encoding! {
symbols: "0123456789abcdefghijklmnopqrstuv",
};

// Base32hex, rfc4648 no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV).
pub const BASE32HEX_NOPAD_UPPER: Encoding = data_encoding::BASE32HEX_NOPAD;

// Base32hex, rfc4648 with padding (alphabet: 0123456789abcdefghijklmnopqrstuv).
pub const BASE32HEX_PAD_LOWER: Encoding = new_encoding! {
symbols: "0123456789abcdefghijklmnopqrstuv",
padding: '=',
};

/// Base32hex, rfc4648 with padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV).
pub const BASE32HEX_PAD_UPPER: Encoding = data_encoding::BASE32HEX;

// z-base-32 (used by Tahoe-LAFS) (alphabet: ybndrfg8ejkmcpqxot1uwisza345h769).
pub const BASE32Z: Encoding = new_encoding! {
symbols: "ybndrfg8ejkmcpqxot1uwisza345h769",
};

// Base58 Flickr's alphabet for creating short urls from photo ids.
pub const BASE58_FLICKR: &str = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

Expand Down
8 changes: 6 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{error, fmt};
#[cfg(not(feature = "std"))]
use core as std;

use std::fmt;
Comment on lines +1 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just use core directly, without special-casing no_std?

So this would just be use core::fmt and further below core::result::Result<T, Error>`.


/// Type alias to use this library's [`Error`] type in a `Result`.
pub type Result<T> = std::result::Result<T, Error>;
Expand All @@ -21,7 +24,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};

pub(crate) trait BaseCodec {
/// Encode with the given byte slice.
fn encode<I: AsRef<[u8]>>(input: I) -> String;
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! # multibase
//!
//! Implementation of [multibase](https://github.com/multiformats/multibase) in Rust.

#![cfg_attr(not(feature = "std"), no_std)]
#![deny(missing_docs)]

#[cfg(not(feature = "std"))]
extern crate alloc;

mod base;
mod encoding;
mod error;
Expand All @@ -12,6 +15,9 @@ mod impls;
pub use self::base::Base;
pub use self::error::{Error, Result};

#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};

/// Decode the base string.
///
/// # Examples
Expand Down