From 3817c6bcab18e98cf4e514b25f5b49a93ca91f60 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sun, 13 Oct 2024 14:36:57 +0400 Subject: [PATCH 1/5] superior approach to marking no_std --- crates/five8/src/decode.rs | 1 + crates/five8/src/encode.rs | 1 + crates/five8/src/lib.rs | 4 +++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/five8/src/decode.rs b/crates/five8/src/decode.rs index bbf9193..ab734bc 100644 --- a/crates/five8/src/decode.rs +++ b/crates/five8/src/decode.rs @@ -282,6 +282,7 @@ mod tests { use prop::array::uniform32; #[cfg(not(miri))] use proptest::prelude::*; + use std::string::String; use super::*; diff --git a/crates/five8/src/encode.rs b/crates/five8/src/encode.rs index 9eb45a2..f205b20 100644 --- a/crates/five8/src/encode.rs +++ b/crates/five8/src/encode.rs @@ -810,6 +810,7 @@ mod tests { use prop::array::uniform32; #[cfg(not(miri))] use proptest::prelude::*; + use std::string::String; use super::*; diff --git a/crates/five8/src/lib.rs b/crates/five8/src/lib.rs index 24d6f4d..feeeaef 100644 --- a/crates/five8/src/lib.rs +++ b/crates/five8/src/lib.rs @@ -1,5 +1,7 @@ #![doc = include_str!("../README.md")] -#![cfg_attr(not(feature = "std"), no_std)] +#![no_std] +#[cfg(feature = "std")] +extern crate std; #[cfg(target_feature = "avx2")] mod avx; From b4efc19c2ce4700d33e3885f7da34b6af337e0b2 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sun, 13 Oct 2024 14:58:33 +0400 Subject: [PATCH 2/5] harmonise std feature activation and add doc_auto_cfg --- crates/five8/CHANGELOG.md | 8 ++++++++ crates/five8/Cargo.toml | 6 +++++- crates/five8/src/lib.rs | 1 + crates/five8_core/Cargo.toml | 4 ++++ crates/five8_core/src/lib.rs | 1 + 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/five8/CHANGELOG.md b/crates/five8/CHANGELOG.md index 1419189..dcb4455 100644 --- a/crates/five8/CHANGELOG.md +++ b/crates/five8/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## [0.2.1] - 2024-10-13 + +- Activate `std` feature of `five8_core` when `std` feature of `five8` is activated [(#7)](https://github.com/kevinheavey/five8/pull/7) + +## [0.2.1] - 2024-10-13 + +- Add feature information to docs via `doc_auto_cfg` [(#7)](https://github.com/kevinheavey/five8/pull/7) + ## [0.2.0] - 2024-09-05 - Remove the `len` parameter from `encode_32` and `encode_64`, and just return the `len`. diff --git a/crates/five8/Cargo.toml b/crates/five8/Cargo.toml index ea170b8..594e9ac 100644 --- a/crates/five8/Cargo.toml +++ b/crates/five8/Cargo.toml @@ -13,7 +13,7 @@ keywords.workspace = true [features] dev-utils = [] # internal use only -std = [] +std = ["five8_core/std"] [dependencies] five8_core.workspace = true @@ -23,3 +23,7 @@ bs58 = "0.5.1" five8 = { path = ".", features = ["std"] } five8_const = { workspace = true } proptest = "1.5.0" + +[package.metadata.docs.rs] +features = ["std"] +rustdoc-args = ["--cfg=docsrs"] diff --git a/crates/five8/src/lib.rs b/crates/five8/src/lib.rs index feeeaef..cbee00b 100644 --- a/crates/five8/src/lib.rs +++ b/crates/five8/src/lib.rs @@ -1,4 +1,5 @@ #![doc = include_str!("../README.md")] +#![cfg_attr(docsrs, feature(doc_auto_cfg))] #![no_std] #[cfg(feature = "std")] extern crate std; diff --git a/crates/five8_core/Cargo.toml b/crates/five8_core/Cargo.toml index bd86d7e..3340b1b 100644 --- a/crates/five8_core/Cargo.toml +++ b/crates/five8_core/Cargo.toml @@ -13,3 +13,7 @@ keywords.workspace = true [features] std = [] + +[package.metadata.docs.rs] +features = ["std"] +rustdoc-args = ["--cfg=docsrs"] diff --git a/crates/five8_core/src/lib.rs b/crates/five8_core/src/lib.rs index 8e67ba4..5015bd5 100644 --- a/crates/five8_core/src/lib.rs +++ b/crates/five8_core/src/lib.rs @@ -1,4 +1,5 @@ #![doc = include_str!("../README.md")] +#![cfg_attr(docsrs, feature(doc_auto_cfg))] pub const BASE58_INVERSE_TABLE_OFFSET: u8 = b'1'; pub const BASE58_INVERSE_TABLE_SENTINEL: u8 = 1 + b'z' - BASE58_INVERSE_TABLE_OFFSET; From 5a49e17e5b15915a6274eb58876a7d73b47604bc Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sun, 13 Oct 2024 15:01:27 +0400 Subject: [PATCH 3/5] missing std import in tests --- crates/five8/src/decode.rs | 6 +++--- crates/five8/src/encode.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/five8/src/decode.rs b/crates/five8/src/decode.rs index ab734bc..be8a088 100644 --- a/crates/five8/src/decode.rs +++ b/crates/five8/src/decode.rs @@ -465,11 +465,11 @@ mod tests { let res2 = unsafe { _mm256_shuffle_epi32::<0b00_00_10_00>(core::mem::transmute(bytes2)) }; let out: [u32; 8] = unsafe { core::mem::transmute(res) }; let out2: [u32; 8] = unsafe { core::mem::transmute(res2) }; - println!("out: {out:?}"); - println!("out2: {out2:?}"); + std::println!("out: {out:?}"); + std::println!("out2: {out2:?}"); let unpacked = unsafe { _mm256_unpacklo_epi64(res, res2) }; let out3: [u32; 8] = unsafe { core::mem::transmute(unpacked) }; - println!("out3: {out3:?}"); + std::println!("out3: {out3:?}"); } #[cfg(not(miri))] diff --git a/crates/five8/src/encode.rs b/crates/five8/src/encode.rs index f205b20..fd8eb57 100644 --- a/crates/five8/src/encode.rs +++ b/crates/five8/src/encode.rs @@ -410,7 +410,7 @@ fn u8s_to_u32s_swapped_64(bytes: &[u8; N_64], out: &mut [u8; N_64]) { // 16, 17, 18, 19, 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, // ) // }; -// println!("mask: {mask:?}"); +// std::println!("mask: {mask:?}"); // let res_m256i = // unsafe { core::arch::x86_64::_mm512_shuffle_epi8(core::mem::transmute(bytes), mask) }; // unsafe { core::mem::transmute(res_m256i) } From eef6e756b3e55813d1e05f9da886192f592569c7 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sun, 13 Oct 2024 15:02:28 +0400 Subject: [PATCH 4/5] make code more concise --- crates/five8_core/src/lib.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/crates/five8_core/src/lib.rs b/crates/five8_core/src/lib.rs index 5015bd5..9eb6f74 100644 --- a/crates/five8_core/src/lib.rs +++ b/crates/five8_core/src/lib.rs @@ -1,5 +1,9 @@ #![doc = include_str!("../README.md")] #![cfg_attr(docsrs, feature(doc_auto_cfg))] +#[cfg(feature = "std")] +extern crate std; +#[cfg(feature = "std")] +use core::fmt; pub const BASE58_INVERSE_TABLE_OFFSET: u8 = b'1'; pub const BASE58_INVERSE_TABLE_SENTINEL: u8 = 1 + b'z' - BASE58_INVERSE_TABLE_OFFSET; @@ -123,18 +127,16 @@ pub enum DecodeError { impl std::error::Error for DecodeError {} #[cfg(feature = "std")] -impl core::fmt::Display for DecodeError { - fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { +impl fmt::Display for DecodeError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { DecodeError::InvalidChar(c) => { - ::core::write!(formatter, "Illegal base58 char number: {}", c) - } - DecodeError::TooLong => formatter.write_str("Base58 string too long"), - DecodeError::TooShort => formatter.write_str("Base58 string too short"), - DecodeError::LargestTermTooHigh => { - formatter.write_str("Largest term greater than 2^32") + write!(f, "Illegal base58 char number: {}", c) } - DecodeError::OutputTooLong => formatter.write_str("Decoded output has too many bytes"), + DecodeError::TooLong => f.write_str("Base58 string too long"), + DecodeError::TooShort => f.write_str("Base58 string too short"), + DecodeError::LargestTermTooHigh => f.write_str("Largest term greater than 2^32"), + DecodeError::OutputTooLong => f.write_str("Decoded output has too many bytes"), } } } From 5b1e7ea7f6d3c8a9af8f7ffa0ce3632fed635841 Mon Sep 17 00:00:00 2001 From: kevinheavey Date: Sun, 13 Oct 2024 15:05:10 +0400 Subject: [PATCH 5/5] lint --- crates/five8/src/decode.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/five8/src/decode.rs b/crates/five8/src/decode.rs index be8a088..f5aedd2 100644 --- a/crates/five8/src/decode.rs +++ b/crates/five8/src/decode.rs @@ -282,7 +282,6 @@ mod tests { use prop::array::uniform32; #[cfg(not(miri))] use proptest::prelude::*; - use std::string::String; use super::*; @@ -478,7 +477,7 @@ mod tests { fn proptest_decode_32(key in uniform32(0u8..)) { let encoded = bs58::encode(key).into_vec(); let bs58_res = bs58::decode(&encoded).into_vec().unwrap(); - let const_res = five8_const::decode_32_const(&String::from_utf8(encoded.clone()).unwrap()); + let const_res = five8_const::decode_32_const(&std::string::String::from_utf8(encoded.clone()).unwrap()); let mut out = [0u8; 32]; decode_32(&encoded, &mut out).unwrap(); assert_eq!(bs58_res, out.to_vec()); @@ -495,7 +494,7 @@ mod tests { combined[32..].copy_from_slice(&second_half); let encoded = bs58::encode(combined).into_vec(); let bs58_res = bs58::decode(&encoded).into_vec().unwrap(); - let const_res = five8_const::decode_64_const(&String::from_utf8(encoded.clone()).unwrap()); + let const_res = five8_const::decode_64_const(&std::string::String::from_utf8(encoded.clone()).unwrap()); let mut out = [0u8; 64]; decode_64(&encoded, &mut out).unwrap(); assert_eq!(bs58_res, out.to_vec());