From 702e175b0a10bf7a2e63dee08473261b4e36835a Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Mon, 12 Aug 2024 07:17:48 +0200 Subject: [PATCH] ctx: Added SizeWith impl for fixed array. (#100) * 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 f338408..c7cf375 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -845,6 +845,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); +}