dusk_bytes/
primitive.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7use crate::{Error, Serializable};
8
9macro_rules! impl_serializable {
10    ($ty:ty) => {
11        impl Serializable<{ core::mem::size_of::<$ty>() }> for $ty {
12            type Error = Error;
13
14            fn from_bytes(buf: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
15                Ok(Self::from_le_bytes(*buf))
16            }
17
18            fn to_bytes(&self) -> [u8; Self::SIZE] {
19                <$ty>::to_le_bytes(*self)
20            }
21        }
22    };
23}
24
25impl_serializable!(u8);
26impl_serializable!(u16);
27impl_serializable!(u32);
28impl_serializable!(u64);
29impl_serializable!(u128);
30
31impl_serializable!(i8);
32impl_serializable!(i16);
33impl_serializable!(i32);
34impl_serializable!(i64);
35impl_serializable!(i128);