From 73c2498fbfc8d6f6c4d93c44768ed8212a5d02e2 Mon Sep 17 00:00:00 2001 From: timofey Date: Fri, 11 Aug 2023 13:30:36 +0000 Subject: [PATCH] `FromRef` --- src/mode.rs | 2 +- src/mode/stream.rs | 8 ++++++++ src/rstd/atomic/array.rs | 2 +- src/rstd/atomic/au64.rs | 2 +- src/rstd/atomic/boolean.rs | 2 +- src/rstd/nullable.rs | 2 +- src/rstd/point.rs | 2 +- 7 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/mode.rs b/src/mode.rs index bd4b071..8c29d0c 100644 --- a/src/mode.rs +++ b/src/mode.rs @@ -24,7 +24,7 @@ pub use self::inlining::InliningMode; pub use self::regular::RegularMode; pub use self::serialization::{Deserializer, DeserializerExt, Serializable, Serializer}; pub use self::slice_deserializer::SliceDeserializer; -pub use self::stream::{Stream, StreamExt, StreamResultExt}; +pub use self::stream::{FromRef, Stream, StreamExt, StreamResultExt}; /// See [`ModeResult`]. pub type ParseSuccess = ::ParseSuccess; diff --git a/src/mode/stream.rs b/src/mode/stream.rs index 5050428..fff0f17 100644 --- a/src/mode/stream.rs +++ b/src/mode/stream.rs @@ -70,3 +70,11 @@ impl StreamResultExt for Result<(A, I), E> { self.map(|(a, _)| a) } } + +pub trait FromRef: for<'a> From<&'a T> { + fn from_ref(value: &T) -> Self { + Self::from(value) + } +} + +impl From<&'a T>> FromRef for U {} diff --git a/src/rstd/atomic/array.rs b/src/rstd/atomic/array.rs index dc8c9b6..2ee73ab 100644 --- a/src/rstd/atomic/array.rs +++ b/src/rstd/atomic/array.rs @@ -48,7 +48,7 @@ impl CInliningAtomic for [u8; N] { } fn ca_ideserialize(stream: I) -> AIParseResult { - stream.iread_n_const::(|slice| ArrayParseError::from(slice)) + stream.iread_n_const::(ArrayParseError::from_ref) } } diff --git a/src/rstd/atomic/au64.rs b/src/rstd/atomic/au64.rs index 3b67fcc..b9421c8 100644 --- a/src/rstd/atomic/au64.rs +++ b/src/rstd/atomic/au64.rs @@ -48,7 +48,7 @@ impl CInliningAtomic for u64 { } fn ca_ideserialize(stream: I) -> AIParseResult { - let (x, stream) = stream.iread_n_const::<8>(|slice| IntParseError::from(slice))?; + let (x, stream) = stream.iread_n_const::<8>(IntParseError::from_ref)?; Ok((u64::from_le_bytes(x), stream)) } } diff --git a/src/rstd/atomic/boolean.rs b/src/rstd/atomic/boolean.rs index 2758371..3068f0d 100644 --- a/src/rstd/atomic/boolean.rs +++ b/src/rstd/atomic/boolean.rs @@ -55,7 +55,7 @@ impl CInliningAtomic for bool { } fn ca_ideserialize(stream: I) -> AIParseResult { - let (byte, stream) = stream.iread_n_const::<1>(|slice| BooleanParseError::from(slice))?; + let (byte, stream) = stream.iread_n_const::<1>(BooleanParseError::from_ref)?; match byte[0] { 0 => Ok((false, stream)), 1 => Ok((true, stream)), diff --git a/src/rstd/nullable.rs b/src/rstd/nullable.rs index 874c628..9010d4d 100644 --- a/src/rstd/nullable.rs +++ b/src/rstd/nullable.rs @@ -126,7 +126,7 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> CInliningFactory<'a, Ctx> fn cideserialize>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> { let factory = self.factory.clone(); - let (address, inctx) = inctx.icnext_address(|slice| PointParseError::from(slice))?; + let (address, inctx) = inctx.icnext_address(PointParseError::from_ref)?; Ok(( if address.point == HASH_ZEROS { Nullable::Null(factory) diff --git a/src/rstd/point.rs b/src/rstd/point.rs index 24ddb2f..e492432 100644 --- a/src/rstd/point.rs +++ b/src/rstd/point.rs @@ -90,7 +90,7 @@ impl<'a, Ctx: Context<'a>, F: FactoryBase<'a, Ctx>> CInliningFactory<'a, Ctx> fo } fn cideserialize>(&self, inctx: I) -> IParseResult<'a, Ctx, Self, I> { - inctx.icnext_point(self.inner(), |slice| PointParseError::from(slice)) + inctx.icnext_point(self.inner(), PointParseError::from_ref) } }