From 5fab3c22eef95620d9726aa80fc558da31dc24be Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 4 Oct 2021 15:13:46 +0200 Subject: [PATCH 1/7] As #[inline] to as_slice --- packages/storage-plus/src/de.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/storage-plus/src/de.rs b/packages/storage-plus/src/de.rs index d7fb24108..da31d3ac8 100644 --- a/packages/storage-plus/src/de.rs +++ b/packages/storage-plus/src/de.rs @@ -14,6 +14,7 @@ pub trait KeyDeserialize { impl KeyDeserialize for () { type Output = (); + #[inline] fn from_slice(_value: &[u8]) -> StdResult { Ok(()) } @@ -22,6 +23,7 @@ impl KeyDeserialize for () { impl KeyDeserialize for Vec { type Output = Vec; + #[inline] fn from_slice(value: &[u8]) -> StdResult { Ok(value.to_vec()) } @@ -30,6 +32,7 @@ impl KeyDeserialize for Vec { impl KeyDeserialize for &Vec { type Output = Vec; + #[inline] fn from_slice(value: &[u8]) -> StdResult { >::from_slice(value) } @@ -38,6 +41,7 @@ impl KeyDeserialize for &Vec { impl KeyDeserialize for &[u8] { type Output = Vec; + #[inline] fn from_slice(value: &[u8]) -> StdResult { >::from_slice(value) } @@ -46,6 +50,7 @@ impl KeyDeserialize for &[u8] { impl KeyDeserialize for String { type Output = String; + #[inline] fn from_slice(value: &[u8]) -> StdResult { String::from_utf8(value.to_vec()) // FIXME: Add and use StdError utf-8 error From helper @@ -56,6 +61,7 @@ impl KeyDeserialize for String { impl KeyDeserialize for &String { type Output = String; + #[inline] fn from_slice(value: &[u8]) -> StdResult { String::from_slice(value) } @@ -64,6 +70,7 @@ impl KeyDeserialize for &String { impl KeyDeserialize for &str { type Output = String; + #[inline] fn from_slice(value: &[u8]) -> StdResult { String::from_slice(value) } @@ -72,6 +79,7 @@ impl KeyDeserialize for &str { impl KeyDeserialize for Addr { type Output = Addr; + #[inline] fn from_slice(value: &[u8]) -> StdResult { Ok(Addr::unchecked(String::from_slice(value)?)) } @@ -80,6 +88,7 @@ impl KeyDeserialize for Addr { impl KeyDeserialize for &Addr { type Output = Addr; + #[inline] fn from_slice(value: &[u8]) -> StdResult { Addr::from_slice(value) } @@ -90,6 +99,7 @@ macro_rules! integer_de { $(impl KeyDeserialize for IntKey<$t> { type Output = $t; + #[inline] fn from_slice(value: &[u8]) -> StdResult { Ok(<$t>::from_be_bytes(value.try_into() // FIXME: Add and use StdError try-from error From helper @@ -104,6 +114,7 @@ integer_de!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128); impl KeyDeserialize for TimestampKey { type Output = u64; + #[inline] fn from_slice(value: &[u8]) -> StdResult { Ok(::from_be_bytes( value @@ -117,6 +128,7 @@ impl KeyDeserialize for TimestampKey { impl KeyDeserialize for (T, U) { type Output = (T::Output, U::Output); + #[inline] fn from_slice(value: &[u8]) -> StdResult { let (len, data) = value.split_at(2); let t_len = u16::from_be_bytes( @@ -133,6 +145,7 @@ impl KeyDeserialize for (T, U) { impl KeyDeserialize for (T, U, V) { type Output = (T::Output, U::Output, V::Output); + #[inline] fn from_slice(value: &[u8]) -> StdResult { let (len, data) = value.split_at(2); let t_len = u16::from_be_bytes( From e64179670cd9eb3a35a533ea886c25d528237884 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 4 Oct 2021 15:17:00 +0200 Subject: [PATCH 2/7] Use inline(always) --- packages/storage-plus/src/de.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/storage-plus/src/de.rs b/packages/storage-plus/src/de.rs index da31d3ac8..d93ed31f1 100644 --- a/packages/storage-plus/src/de.rs +++ b/packages/storage-plus/src/de.rs @@ -14,7 +14,7 @@ pub trait KeyDeserialize { impl KeyDeserialize for () { type Output = (); - #[inline] + #[inline(always)] fn from_slice(_value: &[u8]) -> StdResult { Ok(()) } @@ -23,7 +23,7 @@ impl KeyDeserialize for () { impl KeyDeserialize for Vec { type Output = Vec; - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { Ok(value.to_vec()) } @@ -32,7 +32,7 @@ impl KeyDeserialize for Vec { impl KeyDeserialize for &Vec { type Output = Vec; - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { >::from_slice(value) } @@ -41,7 +41,7 @@ impl KeyDeserialize for &Vec { impl KeyDeserialize for &[u8] { type Output = Vec; - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { >::from_slice(value) } @@ -50,7 +50,7 @@ impl KeyDeserialize for &[u8] { impl KeyDeserialize for String { type Output = String; - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { String::from_utf8(value.to_vec()) // FIXME: Add and use StdError utf-8 error From helper @@ -61,7 +61,7 @@ impl KeyDeserialize for String { impl KeyDeserialize for &String { type Output = String; - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { String::from_slice(value) } @@ -70,7 +70,7 @@ impl KeyDeserialize for &String { impl KeyDeserialize for &str { type Output = String; - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { String::from_slice(value) } @@ -79,7 +79,7 @@ impl KeyDeserialize for &str { impl KeyDeserialize for Addr { type Output = Addr; - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { Ok(Addr::unchecked(String::from_slice(value)?)) } @@ -88,7 +88,7 @@ impl KeyDeserialize for Addr { impl KeyDeserialize for &Addr { type Output = Addr; - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { Addr::from_slice(value) } @@ -99,7 +99,7 @@ macro_rules! integer_de { $(impl KeyDeserialize for IntKey<$t> { type Output = $t; - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { Ok(<$t>::from_be_bytes(value.try_into() // FIXME: Add and use StdError try-from error From helper @@ -114,7 +114,7 @@ integer_de!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128); impl KeyDeserialize for TimestampKey { type Output = u64; - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { Ok(::from_be_bytes( value @@ -128,7 +128,7 @@ impl KeyDeserialize for TimestampKey { impl KeyDeserialize for (T, U) { type Output = (T::Output, U::Output); - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { let (len, data) = value.split_at(2); let t_len = u16::from_be_bytes( @@ -145,7 +145,7 @@ impl KeyDeserialize for (T, U) { impl KeyDeserialize for (T, U, V) { type Output = (T::Output, U::Output, V::Output); - #[inline] + #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { let (len, data) = value.split_at(2); let t_len = u16::from_be_bytes( From be1f414bb1134fa09a060660634cfc98c10ab1e1 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 4 Oct 2021 15:33:33 +0200 Subject: [PATCH 3/7] Use from_vec to save a clone in case of raw keys --- packages/storage-plus/src/de.rs | 60 ++++++++++++++++++++--- packages/storage-plus/src/iter_helpers.rs | 2 +- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/packages/storage-plus/src/de.rs b/packages/storage-plus/src/de.rs index d93ed31f1..46c84fba8 100644 --- a/packages/storage-plus/src/de.rs +++ b/packages/storage-plus/src/de.rs @@ -9,6 +9,7 @@ pub trait KeyDeserialize { type Output: Sized; fn from_slice(value: &[u8]) -> StdResult; + fn from_vec(value: Vec) -> StdResult; } impl KeyDeserialize for () { @@ -18,6 +19,10 @@ impl KeyDeserialize for () { fn from_slice(_value: &[u8]) -> StdResult { Ok(()) } + + fn from_vec(_value: Vec) -> StdResult { + Ok(()) + } } impl KeyDeserialize for Vec { @@ -27,6 +32,10 @@ impl KeyDeserialize for Vec { fn from_slice(value: &[u8]) -> StdResult { Ok(value.to_vec()) } + + fn from_vec(value: Vec) -> StdResult { + Ok(value) + } } impl KeyDeserialize for &Vec { @@ -36,6 +45,10 @@ impl KeyDeserialize for &Vec { fn from_slice(value: &[u8]) -> StdResult { >::from_slice(value) } + + fn from_vec(value: Vec) -> StdResult { + Ok(value) + } } impl KeyDeserialize for &[u8] { @@ -45,6 +58,10 @@ impl KeyDeserialize for &[u8] { fn from_slice(value: &[u8]) -> StdResult { >::from_slice(value) } + + fn from_vec(value: Vec) -> StdResult { + Ok(value) + } } impl KeyDeserialize for String { @@ -56,6 +73,10 @@ impl KeyDeserialize for String { // FIXME: Add and use StdError utf-8 error From helper .map_err(|err| StdError::generic_err(err.to_string())) } + + fn from_vec(value: Vec) -> StdResult { + String::from_slice(value.as_slice()) + } } impl KeyDeserialize for &String { @@ -65,6 +86,10 @@ impl KeyDeserialize for &String { fn from_slice(value: &[u8]) -> StdResult { String::from_slice(value) } + + fn from_vec(value: Vec) -> StdResult { + String::from_slice(value.as_slice()) + } } impl KeyDeserialize for &str { @@ -74,6 +99,10 @@ impl KeyDeserialize for &str { fn from_slice(value: &[u8]) -> StdResult { String::from_slice(value) } + + fn from_vec(value: Vec) -> StdResult { + String::from_slice(value.as_slice()) + } } impl KeyDeserialize for Addr { @@ -83,6 +112,10 @@ impl KeyDeserialize for Addr { fn from_slice(value: &[u8]) -> StdResult { Ok(Addr::unchecked(String::from_slice(value)?)) } + + fn from_vec(value: Vec) -> StdResult { + Addr::from_slice(value.as_slice()) + } } impl KeyDeserialize for &Addr { @@ -92,6 +125,10 @@ impl KeyDeserialize for &Addr { fn from_slice(value: &[u8]) -> StdResult { Addr::from_slice(value) } + + fn from_vec(value: Vec) -> StdResult { + Addr::from_slice(value.as_slice()) + } } macro_rules! integer_de { @@ -105,6 +142,10 @@ macro_rules! integer_de { // FIXME: Add and use StdError try-from error From helper .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?)) } + + fn from_vec(value: Vec) -> StdResult { + >::from_slice(value.as_slice()) + } })* } } @@ -116,12 +157,11 @@ impl KeyDeserialize for TimestampKey { #[inline(always)] fn from_slice(value: &[u8]) -> StdResult { - Ok(::from_be_bytes( - value - .try_into() - // FIXME: Add and use StdError try-from error From helper - .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?, - )) + >::from_slice(value) + } + + fn from_vec(value: Vec) -> StdResult { + >::from_slice(value.as_slice()) } } @@ -140,6 +180,10 @@ impl KeyDeserialize for (T, U) { Ok((T::from_slice(t)?, U::from_slice(u)?)) } + + fn from_vec(value: Vec) -> StdResult { + <(T, U)>::from_slice(value.as_slice()) + } } impl KeyDeserialize for (T, U, V) { @@ -165,6 +209,10 @@ impl KeyDeserialize for Ok((T::from_slice(t)?, U::from_slice(u)?, V::from_slice(v)?)) } + + fn from_vec(value: Vec) -> StdResult { + <(T, U, V)>::from_slice(value.as_slice()) + } } #[cfg(test)] diff --git a/packages/storage-plus/src/iter_helpers.rs b/packages/storage-plus/src/iter_helpers.rs index b2b4ef206..b4d598323 100644 --- a/packages/storage-plus/src/iter_helpers.rs +++ b/packages/storage-plus/src/iter_helpers.rs @@ -19,7 +19,7 @@ pub(crate) fn deserialize_kv( kv: Pair, ) -> StdResult<(K::Output, T)> { let (k, v) = kv; - let kt = K::from_slice(&k)?; + let kt = K::from_vec(k)?; let vt = from_slice::(&v)?; Ok((kt, vt)) } From ff351b85fd88851ae55be9a2bc4abf974216ea78 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 4 Oct 2021 15:35:39 +0200 Subject: [PATCH 4/7] Inline always from_vec too --- packages/storage-plus/src/de.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/storage-plus/src/de.rs b/packages/storage-plus/src/de.rs index 46c84fba8..9d3ded978 100644 --- a/packages/storage-plus/src/de.rs +++ b/packages/storage-plus/src/de.rs @@ -20,6 +20,7 @@ impl KeyDeserialize for () { Ok(()) } + #[inline(always)] fn from_vec(_value: Vec) -> StdResult { Ok(()) } @@ -33,6 +34,7 @@ impl KeyDeserialize for Vec { Ok(value.to_vec()) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { Ok(value) } @@ -46,6 +48,7 @@ impl KeyDeserialize for &Vec { >::from_slice(value) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { Ok(value) } @@ -59,6 +62,7 @@ impl KeyDeserialize for &[u8] { >::from_slice(value) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { Ok(value) } @@ -74,6 +78,7 @@ impl KeyDeserialize for String { .map_err(|err| StdError::generic_err(err.to_string())) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { String::from_slice(value.as_slice()) } @@ -87,6 +92,7 @@ impl KeyDeserialize for &String { String::from_slice(value) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { String::from_slice(value.as_slice()) } @@ -100,6 +106,7 @@ impl KeyDeserialize for &str { String::from_slice(value) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { String::from_slice(value.as_slice()) } @@ -113,6 +120,7 @@ impl KeyDeserialize for Addr { Ok(Addr::unchecked(String::from_slice(value)?)) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { Addr::from_slice(value.as_slice()) } @@ -126,6 +134,7 @@ impl KeyDeserialize for &Addr { Addr::from_slice(value) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { Addr::from_slice(value.as_slice()) } @@ -143,6 +152,7 @@ macro_rules! integer_de { .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?)) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { >::from_slice(value.as_slice()) } @@ -160,6 +170,7 @@ impl KeyDeserialize for TimestampKey { >::from_slice(value) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { >::from_slice(value.as_slice()) } @@ -181,6 +192,7 @@ impl KeyDeserialize for (T, U) { Ok((T::from_slice(t)?, U::from_slice(u)?)) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { <(T, U)>::from_slice(value.as_slice()) } @@ -210,6 +222,7 @@ impl KeyDeserialize for Ok((T::from_slice(t)?, U::from_slice(u)?, V::from_slice(v)?)) } + #[inline(always)] fn from_vec(value: Vec) -> StdResult { <(T, U, V)>::from_slice(value.as_slice()) } From 97fcb6c55a0cfaba167c81f47c6fbbbc41d11c90 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 4 Oct 2021 15:55:21 +0200 Subject: [PATCH 5/7] Move impl to from_vec Add as_slice as default trait impl --- packages/storage-plus/src/de.rs | 92 ++++++--------------------------- 1 file changed, 15 insertions(+), 77 deletions(-) diff --git a/packages/storage-plus/src/de.rs b/packages/storage-plus/src/de.rs index 9d3ded978..1f661d6aa 100644 --- a/packages/storage-plus/src/de.rs +++ b/packages/storage-plus/src/de.rs @@ -8,18 +8,16 @@ use crate::keys::{IntKey, TimestampKey}; pub trait KeyDeserialize { type Output: Sized; - fn from_slice(value: &[u8]) -> StdResult; fn from_vec(value: Vec) -> StdResult; + + fn from_slice(value: &[u8]) -> StdResult { + Self::from_vec(value.to_vec()) + } } impl KeyDeserialize for () { type Output = (); - #[inline(always)] - fn from_slice(_value: &[u8]) -> StdResult { - Ok(()) - } - #[inline(always)] fn from_vec(_value: Vec) -> StdResult { Ok(()) @@ -29,11 +27,6 @@ impl KeyDeserialize for () { impl KeyDeserialize for Vec { type Output = Vec; - #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { - Ok(value.to_vec()) - } - #[inline(always)] fn from_vec(value: Vec) -> StdResult { Ok(value) @@ -43,11 +36,6 @@ impl KeyDeserialize for Vec { impl KeyDeserialize for &Vec { type Output = Vec; - #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { - >::from_slice(value) - } - #[inline(always)] fn from_vec(value: Vec) -> StdResult { Ok(value) @@ -57,11 +45,6 @@ impl KeyDeserialize for &Vec { impl KeyDeserialize for &[u8] { type Output = Vec; - #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { - >::from_slice(value) - } - #[inline(always)] fn from_vec(value: Vec) -> StdResult { Ok(value) @@ -72,71 +55,46 @@ impl KeyDeserialize for String { type Output = String; #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { - String::from_utf8(value.to_vec()) + fn from_vec(value: Vec) -> StdResult { + String::from_utf8(value) // FIXME: Add and use StdError utf-8 error From helper .map_err(|err| StdError::generic_err(err.to_string())) } - - #[inline(always)] - fn from_vec(value: Vec) -> StdResult { - String::from_slice(value.as_slice()) - } } impl KeyDeserialize for &String { type Output = String; - #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { - String::from_slice(value) - } - #[inline(always)] fn from_vec(value: Vec) -> StdResult { - String::from_slice(value.as_slice()) + String::from_vec(value) } } impl KeyDeserialize for &str { type Output = String; - #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { - String::from_slice(value) - } - #[inline(always)] fn from_vec(value: Vec) -> StdResult { - String::from_slice(value.as_slice()) + String::from_vec(value) } } impl KeyDeserialize for Addr { type Output = Addr; - #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { - Ok(Addr::unchecked(String::from_slice(value)?)) - } - #[inline(always)] fn from_vec(value: Vec) -> StdResult { - Addr::from_slice(value.as_slice()) + Ok(Addr::unchecked(String::from_vec(value)?)) } } impl KeyDeserialize for &Addr { type Output = Addr; - #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { - Addr::from_slice(value) - } - #[inline(always)] fn from_vec(value: Vec) -> StdResult { - Addr::from_slice(value.as_slice()) + Addr::from_vec(value) } } @@ -146,16 +104,11 @@ macro_rules! integer_de { type Output = $t; #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { - Ok(<$t>::from_be_bytes(value.try_into() + fn from_vec(value: Vec) -> StdResult { + Ok(<$t>::from_be_bytes(value.as_slice().try_into() // FIXME: Add and use StdError try-from error From helper .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?)) } - - #[inline(always)] - fn from_vec(value: Vec) -> StdResult { - >::from_slice(value.as_slice()) - } })* } } @@ -165,14 +118,9 @@ integer_de!(for i8, u8, i16, u16, i32, u32, i64, u64, i128, u128); impl KeyDeserialize for TimestampKey { type Output = u64; - #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { - >::from_slice(value) - } - #[inline(always)] fn from_vec(value: Vec) -> StdResult { - >::from_slice(value.as_slice()) + >::from_vec(value) } } @@ -180,7 +128,7 @@ impl KeyDeserialize for (T, U) { type Output = (T::Output, U::Output); #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { + fn from_vec(value: Vec) -> StdResult { let (len, data) = value.split_at(2); let t_len = u16::from_be_bytes( len.try_into() @@ -191,18 +139,13 @@ impl KeyDeserialize for (T, U) { Ok((T::from_slice(t)?, U::from_slice(u)?)) } - - #[inline(always)] - fn from_vec(value: Vec) -> StdResult { - <(T, U)>::from_slice(value.as_slice()) - } } impl KeyDeserialize for (T, U, V) { type Output = (T::Output, U::Output, V::Output); #[inline(always)] - fn from_slice(value: &[u8]) -> StdResult { + fn from_vec(value: Vec) -> StdResult { let (len, data) = value.split_at(2); let t_len = u16::from_be_bytes( len.try_into() @@ -221,11 +164,6 @@ impl KeyDeserialize for Ok((T::from_slice(t)?, U::from_slice(u)?, V::from_slice(v)?)) } - - #[inline(always)] - fn from_vec(value: Vec) -> StdResult { - <(T, U, V)>::from_slice(value.as_slice()) - } } #[cfg(test)] From c4c8af2afdd576cd624a5cb6f97db6fc205e9ebe Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 4 Oct 2021 16:07:26 +0200 Subject: [PATCH 6/7] Use Self::Output in forwarding impls --- packages/storage-plus/src/de.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/storage-plus/src/de.rs b/packages/storage-plus/src/de.rs index 1f661d6aa..3f33bf583 100644 --- a/packages/storage-plus/src/de.rs +++ b/packages/storage-plus/src/de.rs @@ -67,7 +67,7 @@ impl KeyDeserialize for &String { #[inline(always)] fn from_vec(value: Vec) -> StdResult { - String::from_vec(value) + Self::Output::from_vec(value) } } @@ -76,7 +76,7 @@ impl KeyDeserialize for &str { #[inline(always)] fn from_vec(value: Vec) -> StdResult { - String::from_vec(value) + Self::Output::from_vec(value) } } @@ -94,7 +94,7 @@ impl KeyDeserialize for &Addr { #[inline(always)] fn from_vec(value: Vec) -> StdResult { - Addr::from_vec(value) + Self::Output::from_vec(value) } } @@ -120,7 +120,7 @@ impl KeyDeserialize for TimestampKey { #[inline(always)] fn from_vec(value: Vec) -> StdResult { - >::from_vec(value) + >::from_vec(value) } } From 2629245c77ec49f98b32736602fd2bece8b74c80 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Mon, 4 Oct 2021 17:48:37 +0200 Subject: [PATCH 7/7] Use split_off instead of split_at --- packages/storage-plus/src/de.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/storage-plus/src/de.rs b/packages/storage-plus/src/de.rs index 3f33bf583..54dfa4cf7 100644 --- a/packages/storage-plus/src/de.rs +++ b/packages/storage-plus/src/de.rs @@ -128,16 +128,18 @@ impl KeyDeserialize for (T, U) { type Output = (T::Output, U::Output); #[inline(always)] - fn from_vec(value: Vec) -> StdResult { - let (len, data) = value.split_at(2); + fn from_vec(mut value: Vec) -> StdResult { + let mut tu = value.split_off(2); let t_len = u16::from_be_bytes( - len.try_into() + value + .as_slice() + .try_into() // FIXME: Add and use StdError try-from error From helper .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?, ) as usize; - let (t, u) = data.split_at(t_len); + let u = tu.split_off(t_len); - Ok((T::from_slice(t)?, U::from_slice(u)?)) + Ok((T::from_vec(tu)?, U::from_vec(u)?)) } } @@ -145,24 +147,28 @@ impl KeyDeserialize for type Output = (T::Output, U::Output, V::Output); #[inline(always)] - fn from_vec(value: Vec) -> StdResult { - let (len, data) = value.split_at(2); + fn from_vec(mut value: Vec) -> StdResult { + let mut tuv = value.split_off(2); let t_len = u16::from_be_bytes( - len.try_into() + value + .as_slice() + .try_into() // FIXME: Add and use StdError try-from error From helper .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?, ) as usize; - let (t, data) = data.split_at(t_len); + let mut len_uv = tuv.split_off(t_len); - let (len, data) = data.split_at(2); + let mut uv = len_uv.split_off(2); let u_len = u16::from_be_bytes( - len.try_into() + len_uv + .as_slice() + .try_into() // FIXME: Add and use StdError try-from error From helper .map_err(|err: TryFromSliceError| StdError::generic_err(err.to_string()))?, ) as usize; - let (u, v) = data.split_at(u_len); + let v = uv.split_off(u_len); - Ok((T::from_slice(t)?, U::from_slice(u)?, V::from_slice(v)?)) + Ok((T::from_vec(tuv)?, U::from_vec(uv)?, V::from_vec(v)?)) } }