feat: support no-std for reth-codecs (#5756)

This commit is contained in:
yjh
2023-12-18 16:07:25 +08:00
committed by GitHub
parent fcdd31a6f4
commit bb6f6f43c2
2 changed files with 13 additions and 5 deletions

View File

@ -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"]

View File

@ -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::*;
@ -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]
@ -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)
}
@ -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)
}
}