From 7916216eb287a8f72d89fc73cfb19c8c8062ce43 Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Mon, 10 Jun 2024 11:04:25 +0200 Subject: [PATCH 1/2] Added SizeWith impl for fixed array. --- src/ctx.rs | 5 +++++ tests/api.rs | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ctx.rs b/src/ctx.rs index e24d2dc..05948d9 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -830,6 +830,11 @@ impl, const N: usize> TryInt Ok(offset) } } +impl, const N: usize> SizeWith for [T; N] { + fn size_with(ctx: &Ctx) -> usize { + T::size_with(ctx) * N + } +} #[cfg(feature = "std")] impl<'a> TryFromCtx<'a> for &'a CStr { diff --git a/tests/api.rs b/tests/api.rs index b44c726..1e603f1 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -3,7 +3,7 @@ use std::ops::{Deref, DerefMut}; use scroll::ctx::SizeWith as _; -use scroll::{ctx, Cread, Pread, Result}; +use scroll::{ctx, Cread, Endian, Pread, Result}; #[derive(Default)] pub struct Section<'a> { @@ -376,3 +376,8 @@ fn test_fixed_array_rw() { buf.pwrite([0x1337u16, 0x1337], 0).unwrap(); assert_eq!(buf, bytes); } + +#[test] +fn test_fixed_array_size_with() { + assert_eq!(<[u32; 3]>::size_with(&Endian::Little), 12); +} From 6ab543602d7989d18fe41336118d00c051d0d25e Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Mon, 10 Jun 2024 11:06:05 +0200 Subject: [PATCH 2/2] Removed unnecessary trait bound. --- src/ctx.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ctx.rs b/src/ctx.rs index 05948d9..6b20dc8 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -830,7 +830,7 @@ impl, const N: usize> TryInt Ok(offset) } } -impl, const N: usize> SizeWith for [T; N] { +impl, const N: usize> SizeWith for [T; N] { fn size_with(ctx: &Ctx) -> usize { T::size_with(ctx) * N }