Skip to content

Commit d274374

Browse files
committed
feat: trait FromByte add SIZE
1 parent c425c49 commit d274374

File tree

5 files changed

+11
-5
lines changed

5 files changed

+11
-5
lines changed

Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ resolver = "2"
2222

2323
[workspace.package]
2424
authors = ["Yvictor <[email protected]>"]
25-
version = "0.1.12"
25+
version = "0.1.13"
2626
edition = "2021"
2727
license = "MIT"
2828
repository = "https://github.com/Yvictor/binary_mirror"

binary-mirror-derive/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -974,8 +974,10 @@ fn impl_binary_mirror(input: &DeriveInput) -> TokenStream {
974974
#native_struct_code
975975

976976
impl binary_mirror::FromBytes for #name {
977+
const SIZE: usize = std::mem::size_of::<Self>();
978+
977979
fn from_bytes(bytes: &[u8]) -> Result<&Self, binary_mirror::BytesSizeError> {
978-
let expected = Self::size();
980+
let expected = Self::SIZE;
979981
let actual = bytes.len();
980982
if actual != expected {
981983
return Err(binary_mirror::BytesSizeError::new(

binary-mirror-derive/tests/derive_tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -857,4 +857,5 @@ fn test_skipped_fields() {
857857
let raw2 = native2.to_raw();
858858
assert_eq!(raw.to_bytes(), raw2.to_bytes());
859859
assert_eq!(WithSkippedFields::size(), 34);
860+
assert_eq!(WithSkippedFields::SIZE, 34);
860861
}

binary-mirror/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ pub struct FieldSpec {
5353
}
5454

5555
pub trait FromBytes: Sized {
56+
/// Get the size of the struct in bytes
57+
const SIZE: usize;
58+
5659
/// Create a new instance from bytes
5760
/// Returns Err if the bytes length doesn't match the struct size
5861
fn from_bytes(bytes: &[u8]) -> Result<&Self, BytesSizeError>;

0 commit comments

Comments
 (0)