From de0a303a360757bc4c5588212aae23f7414f5fb5 Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Sun, 13 Jun 2021 17:26:18 +0200
Subject: [PATCH] feat(kv_typed): impl FromBytes for all SmallVec (use const
 generics)

---
 tools/kv_typed/src/from_bytes.rs | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)

diff --git a/tools/kv_typed/src/from_bytes.rs b/tools/kv_typed/src/from_bytes.rs
index 5ae7245..6cade2d 100644
--- a/tools/kv_typed/src/from_bytes.rs
+++ b/tools/kv_typed/src/from_bytes.rs
@@ -55,23 +55,19 @@ impl FromBytes for String {
     }
 }
 
-macro_rules! impl_from_bytes_for_smallvec {
-    ($($N:literal),*) => {$(
-        impl<T> FromBytes for SmallVec<[T; $N]>
-        where
-            T: Copy + zerocopy::FromBytes,
-        {
-            type Err = LayoutVerifiedErr;
+impl<T, const N: usize> FromBytes for SmallVec<[T; N]>
+where
+    T: Copy + zerocopy::FromBytes,
+    [T; N]: smallvec::Array<Item = T>,
+{
+    type Err = LayoutVerifiedErr;
 
-            fn from_bytes(bytes: &[u8]) -> Result<Self, Self::Err> {
-                let layout_verified = zerocopy::LayoutVerified::<_, [T]>::new_slice(bytes)
-                    .ok_or_else(|| LayoutVerifiedErr(stringify!(T)).into())?;
-                Ok(SmallVec::from_slice(layout_verified.into_slice()))
-            }
-        }
-    )*};
+    fn from_bytes(bytes: &[u8]) -> Result<Self, Self::Err> {
+        let layout_verified = zerocopy::LayoutVerified::<_, [T]>::new_slice(bytes)
+            .ok_or(LayoutVerifiedErr(stringify!(T)))?;
+        Ok(SmallVec::from_slice(layout_verified.into_slice()))
+    }
 }
-impl_from_bytes_for_smallvec!(1, 2, 4, 8, 16, 32, 64);
 
 impl<T> FromBytes for Vec<T>
 where
-- 
GitLab