Skip to content

Commit

Permalink
support no-std for reth-codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
yjhmelody committed Dec 14, 2023
1 parent 013b4c9 commit af2e7af
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion crates/storage/codecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ proptest.workspace = true
proptest-derive.workspace = true

[features]
default = ["compact"]
default = ["compact", "std"]
std = [
"alloy-primitives/std",
"bytes/std",
]
compact = ["codecs-derive/compact"]
scale = ["codecs-derive/scale"]
postcard = ["codecs-derive/postcard"]
Expand Down
12 changes: 8 additions & 4 deletions crates/storage/codecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![allow(clippy::non_canonical_clone_impl)]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;
use alloc::vec::Vec;

pub use codecs_derive::*;

Expand Down Expand Up @@ -78,7 +82,7 @@ macro_rules! impl_uint_compact {
{
let leading = self.leading_zeros() as usize / 8;
buf.put_slice(&self.to_be_bytes()[leading..]);
std::mem::size_of::<$name>() - leading
core::mem::size_of::<$name>() - leading
}

#[inline]
Expand All @@ -87,8 +91,8 @@ macro_rules! impl_uint_compact {
return (0, buf);
}

let mut arr = [0; std::mem::size_of::<$name>()];
arr[std::mem::size_of::<$name>() - len..].copy_from_slice(&buf[..len]);
let mut arr = [0; core::mem::size_of::<$name>()];
arr[core::mem::size_of::<$name>() - len..].copy_from_slice(&buf[..len]);
buf.advance(len);
($name::from_be_bytes(arr), buf)
}
Expand Down Expand Up @@ -317,7 +321,7 @@ macro_rules! impl_compact_for_bytes {

#[inline]
fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (v, buf) = <[u8; std::mem::size_of::<$name>()]>::from_compact(buf, len);
let (v, buf) = <[u8; core::mem::size_of::<$name>()]>::from_compact(buf, len);
(Self::from(v), buf)
}
}
Expand Down

0 comments on commit af2e7af

Please sign in to comment.