From 604a2929842312a04a7963355585f16ec8bca7a1 Mon Sep 17 00:00:00 2001 From: mriise Date: Sun, 23 Nov 2025 02:17:17 -0800 Subject: [PATCH 1/3] feat: support no_std --- Cargo.toml | 3 ++- src/decode.rs | 13 ++++++++----- src/encode.rs | 4 +++- src/lib.rs | 5 +++++ src/tests.rs | 2 ++ 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 70fd2cd..6a79bd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ name = "bench" harness = false [features] -# depcrecated, will be removed in a future release +default = ["std"] +std = [] array_chunks = [] 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..7314dbe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(feature = "array_chunks", feature(array_chunks))] +#![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 +16,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..917f50f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,4 +1,6 @@ use crate::*; +#[cfg(not(feature = "std"))] +use alloc::{vec, string::String}; #[test] fn encode_ab() { From 94c5762e4d4023e9bac324e54ab8cb98fa6c6b1b Mon Sep 17 00:00:00 2001 From: Rickard Natt och Dag Date: Tue, 16 Dec 2025 22:43:21 +0100 Subject: [PATCH 2/3] chore: remove array_chunks --- Cargo.toml | 1 - src/lib.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6a79bd7..1797e08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,5 +24,4 @@ harness = false [features] default = ["std"] std = [] -array_chunks = [] unsafe = [] diff --git a/src/lib.rs b/src/lib.rs index 7314dbe..a3dc740 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -#![cfg_attr(feature = "array_chunks", feature(array_chunks))] #![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) From 6b3d8ae862ed1db3c97e4c40e4e7628424d113f1 Mon Sep 17 00:00:00 2001 From: Rickard Natt och Dag Date: Tue, 16 Dec 2025 22:47:17 +0100 Subject: [PATCH 3/3] chore: formatting --- src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests.rs b/src/tests.rs index 917f50f..09d5137 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,6 +1,6 @@ use crate::*; #[cfg(not(feature = "std"))] -use alloc::{vec, string::String}; +use alloc::{string::String, vec}; #[test] fn encode_ab() {