diff --git a/Cargo.toml b/Cargo.toml index 70fd2cd..1797e08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,6 @@ name = "bench" harness = false [features] -# depcrecated, will be removed in a future release -array_chunks = [] +default = ["std"] +std = [] unsafe = [] diff --git a/src/decode.rs b/src/decode.rs index 07c3361..3bd6e52 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -1,24 +1,27 @@ use crate::alphabet::{self, SIZE, SIZE_SIZE}; -use std::error::Error; -use std::fmt::{Display, Formatter}; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + +use core::fmt::{Display, Formatter}; /// The error type returned when the input is not a valid base45 string #[derive(Eq, PartialEq, Copy, Clone, Debug)] pub struct DecodeError; impl Display for DecodeError { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { f.write_str("Invalid base45 string") } } -impl Error for DecodeError {} +impl core::error::Error for DecodeError {} /// Decode a string from base45 /// /// Takes a base45 encoded string and returns a UTF-8 string on success /// /// ```rust,no_run -/// # fn main() -> Result<(), Box> { +/// # fn main() -> Result<(), Box> { /// let decoded = String::from_utf8(base45::decode("%69 VD92EX0")?)?; /// assert_eq!(decoded, "Hello!!"); /// # Ok(()) diff --git a/src/encode.rs b/src/encode.rs index 0d2f31a..68581ba 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -1,6 +1,8 @@ use crate::alphabet::{self, SIZE, SIZE_SIZE}; -#[inline(always)] +#[cfg(not(feature = "std"))] +use alloc::{string::String, vec::Vec}; + fn divmod(x: u32) -> (u32, u32) { (x / N, x % N) } diff --git a/src/lib.rs b/src/lib.rs index bd321ba..a3dc740 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![cfg_attr(not(feature = "std"), no_std)] //! Encoder/decoder for base45 that is fully compatible with //! [`draft-faltstrom-base45-07.txt`](https://www.ietf.org/archive/id/draft-faltstrom-base45-07.txt) //! @@ -14,6 +15,9 @@ //! [`core::slice::ChunksExact`](https://doc.rust-lang.org/core/slice/struct.ChunksExact.html). //! Ideally, there is no performance penalty using this means. +#[cfg(not(feature = "std"))] +extern crate alloc; + pub mod alphabet; mod decode; mod encode; diff --git a/src/tests.rs b/src/tests.rs index 35de6aa..09d5137 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,4 +1,6 @@ use crate::*; +#[cfg(not(feature = "std"))] +use alloc::{string::String, vec}; #[test] fn encode_ab() {