From e76f339c2d3a5ea205865f44799a486bdf8c0d57 Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Fri, 16 Aug 2024 21:27:41 +0200 Subject: [PATCH 1/2] Added TryFromCtx impl for &[u8] with no ctx. --- src/ctx.rs | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/ctx.rs b/src/ctx.rs index c01dfbf..edb7fef 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -731,6 +731,29 @@ impl<'a> TryIntoCtx for &'a [u8] { } } +impl<'a> TryFromCtx<'a, usize> for &'a [u8] { + type Error = error::Error; + #[inline] + fn try_from_ctx(src: &'a [u8], size: usize) -> result::Result<(Self, usize), Self::Error> { + if size > src.len() { + Err(error::Error::TooBig { + size, + len: src.len(), + }) + } else { + Ok((&src[..size], size)) + } + } +} + +impl<'a> TryFromCtx<'a> for &'a [u8] { + type Error = error::Error; + #[inline] + fn try_from_ctx(src: &'a [u8], _ctx: ()) -> result::Result<(Self, usize), Self::Error> { + Ok((src, src.len())) + } +} + // TODO: make TryIntoCtx use StrCtx for awesomeness impl<'a> TryIntoCtx for &'a str { type Error = error::Error; @@ -772,20 +795,6 @@ sizeof_impl!(i128); sizeof_impl!(f32); sizeof_impl!(f64); -impl<'a> TryFromCtx<'a, usize> for &'a [u8] { - type Error = error::Error; - #[inline] - fn try_from_ctx(src: &'a [u8], size: usize) -> result::Result<(Self, usize), Self::Error> { - if size > src.len() { - Err(error::Error::TooBig { - size, - len: src.len(), - }) - } else { - Ok((&src[..size], size)) - } - } -} impl<'a, Ctx: Copy, T: TryFromCtx<'a, Ctx, Error = error::Error>, const N: usize> TryFromCtx<'a, Ctx> for [T; N] From 2d71cd455f9cfd9d38d5f7ee73b5d300965465f8 Mon Sep 17 00:00:00 2001 From: Frostie314159 Date: Fri, 16 Aug 2024 21:34:31 +0200 Subject: [PATCH 2/2] rustfmt --- src/ctx.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ctx.rs b/src/ctx.rs index edb7fef..e849f90 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -795,7 +795,6 @@ sizeof_impl!(i128); sizeof_impl!(f32); sizeof_impl!(f64); - impl<'a, Ctx: Copy, T: TryFromCtx<'a, Ctx, Error = error::Error>, const N: usize> TryFromCtx<'a, Ctx> for [T; N] {