From d718f226b81401eb2376a5bd0a56e1606319d59d Mon Sep 17 00:00:00 2001 From: Ted Mielczarek Date: Thu, 13 Sep 2018 13:25:34 -0400 Subject: [PATCH] Implement IntoCtx/TryIntoCtx for references to primitive types. This should help in fixing https://github.com/m4b/scroll_derive/issues/15 . --- src/ctx.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ctx.rs b/src/ctx.rs index 6a42281..fdfb763 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -188,6 +188,12 @@ macro_rules! into_ctx_impl { write_into!($typ, $size, self, dst, le); } } + impl<'a> IntoCtx for &'a $typ { + #[inline] + fn into_ctx(self, dst: &mut [u8], le: Endian) { + (*self).into_ctx(dst, le) + } + } impl TryIntoCtx for $typ where $typ: IntoCtx { type Error = error::Error; type Size = usize; @@ -201,6 +207,14 @@ macro_rules! into_ctx_impl { } } } + impl<'a> TryIntoCtx for &'a $typ { + type Error = error::Error; + type Size = usize; + #[inline] + fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { + (*self).try_into_ctx(dst, le) + } + } } } @@ -295,7 +309,6 @@ macro_rules! from_ctx_float_impl { } } } - impl<'a> TryFromCtx<'a, Endian> for $typ where $typ: FromCtx { type Error = error::Error; type Size = usize; @@ -334,6 +347,12 @@ macro_rules! into_ctx_float_impl { write_into!(signed_to_unsigned!($typ), $size, transmute::<$typ, signed_to_unsigned!($typ)>(self), dst, le); } } + impl<'a> IntoCtx for &'a $typ { + #[inline] + fn into_ctx(self, dst: &mut [u8], le: Endian) { + (*self).into_ctx(dst, le) + } + } impl TryIntoCtx for $typ where $typ: IntoCtx { type Error = error::Error; type Size = usize; @@ -347,6 +366,14 @@ macro_rules! into_ctx_float_impl { } } } + impl<'a> TryIntoCtx for &'a $typ { + type Error = error::Error; + type Size = usize; + #[inline] + fn try_into_ctx(self, dst: &mut [u8], le: Endian) -> error::Result { + (*self).try_into_ctx(dst, le) + } + } } }