Skip to content

Commit

Permalink
allow for no_std (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: bwty <whalelephant@users.noreply.github.com>
  • Loading branch information
whalelephant and whalelephant committed Nov 3, 2020
1 parent 15dfd66 commit 8f46fda
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ rand = "0.3"
[[bench]]
name = "base"
harness = false

[features]
default = ["std"]
std = []
2 changes: 2 additions & 0 deletions src/alphabet.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use DecodeError;

use decoder::*;
Expand Down
6 changes: 6 additions & 0 deletions src/bigint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[cfg(not(feature = "std"))]
use core as std;

use std::{ptr, u32};

/// This is a pretty naive implementation of a BigUint abstracting all
Expand Down
10 changes: 7 additions & 3 deletions src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[cfg(not(feature = "std"))]
use core as std;

use bigint::BigUint;
use DecodeError;

Expand Down Expand Up @@ -43,7 +49,6 @@ where
}
}


pub(crate) struct U8Decoder<'b> {
alphabet: &'b [u8],
lookup: [u8; 256],
Expand All @@ -60,7 +65,6 @@ impl<'a> U8Decoder<'a> {
}
U8Decoder { alphabet, lookup }
}

}

impl<'a, 'b> Decoder<'a, 'b> for U8Decoder<'b> {
Expand Down Expand Up @@ -109,4 +113,4 @@ impl<'a, 'b> Decoder<'a, 'b> for CharDecoder<'b> {
{
self.0
}
}
}
2 changes: 2 additions & 0 deletions src/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use bigint::BigUint;

pub(crate) fn encode<T>(alpha: &[T], input: &[u8]) -> Vec<T>
Expand Down
15 changes: 13 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@
//! }
//! ```

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

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

pub mod alphabet;
mod bigint;
pub mod decoder;
pub mod encoder;

pub use alphabet::Alphabet;

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

#[cfg(not(feature = "std"))]
use core as std;

use std::fmt;

#[derive(Debug)]
Expand All @@ -42,7 +52,8 @@ impl fmt::Display for DecodeError {
}
}

impl Error for DecodeError {
#[cfg(features = "std")]
impl std::error::Error for DecodeError {
fn description(&self) -> &str {
"Can not decode the provided data"
}
Expand Down

0 comments on commit 8f46fda

Please sign in to comment.