Skip to content

Commit 4337b1b

Browse files
Fuuzetsubluss
authored andcommitted
Optional support for borsh serialization
1 parent 7a1722e commit 4337b1b

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ categories = ["data-structures", "no-std"]
1414

1515
[build-dependencies]
1616

17+
[dependencies.borsh]
18+
version = "1.2.0"
19+
optional = true
20+
default-features = false
21+
1722
[dependencies.serde]
1823
version = "1.0"
1924
optional = true
@@ -49,7 +54,7 @@ debug = true
4954
debug = true
5055

5156
[package.metadata.docs.rs]
52-
features = ["serde", "zeroize"]
57+
features = ["borsh", "serde", "zeroize"]
5358

5459
[package.metadata.release]
5560
no-dev-version = true

src/array_string.rs

+22
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,28 @@ impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
628628
}
629629
}
630630

631+
#[cfg(feature = "borsh")]
632+
/// Requires crate feature `"borsh"`
633+
impl<const CAP: usize> borsh::BorshSerialize for ArrayString<CAP> {
634+
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
635+
<str as borsh::BorshSerialize>::serialize(&*self, writer)
636+
}
637+
}
638+
639+
#[cfg(feature = "borsh")]
640+
/// Requires crate feature `"borsh"`
641+
impl<const CAP: usize> borsh::BorshDeserialize for ArrayString<CAP> {
642+
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
643+
let s = <String as borsh::BorshDeserialize>::deserialize_reader(reader)?;
644+
ArrayString::from(&s).map_err(|_| {
645+
borsh::io::Error::new(
646+
borsh::io::ErrorKind::InvalidData,
647+
format!("expected a string no more than {} bytes long", CAP),
648+
)
649+
})
650+
}
651+
}
652+
631653
impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
632654
{
633655
type Error = CapacityError<&'a str>;

src/arrayvec.rs

+39
Original file line numberDiff line numberDiff line change
@@ -1301,3 +1301,42 @@ impl<'de, T: Deserialize<'de>, const CAP: usize> Deserialize<'de> for ArrayVec<T
13011301
deserializer.deserialize_seq(ArrayVecVisitor::<T, CAP>(PhantomData))
13021302
}
13031303
}
1304+
1305+
#[cfg(feature = "borsh")]
1306+
/// Requires crate feature `"borsh"`
1307+
impl<T, const CAP: usize> borsh::BorshSerialize for ArrayVec<T, CAP>
1308+
where
1309+
T: borsh::BorshSerialize,
1310+
{
1311+
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
1312+
let vs = self.as_slice();
1313+
<usize as borsh::BorshSerialize>::serialize(&vs.len(), writer)?;
1314+
for elem in vs {
1315+
<T as borsh::BorshSerialize>::serialize(elem, writer)?;
1316+
}
1317+
Ok(())
1318+
}
1319+
}
1320+
1321+
#[cfg(feature = "borsh")]
1322+
/// Requires crate feature `"borsh"`
1323+
impl<T, const CAP: usize> borsh::BorshDeserialize for ArrayVec<T, CAP>
1324+
where
1325+
T: borsh::BorshDeserialize,
1326+
{
1327+
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
1328+
let mut values = Self::new();
1329+
let len = <usize as borsh::BorshDeserialize>::deserialize_reader(reader)?;
1330+
for _ in 0..len {
1331+
let elem = <T as borsh::BorshDeserialize>::deserialize_reader(reader)?;
1332+
if let Err(_) = values.try_push(elem) {
1333+
return Err(borsh::io::Error::new(
1334+
borsh::io::ErrorKind::InvalidData,
1335+
format!("expected an array with no more than {} items", CAP),
1336+
));
1337+
}
1338+
}
1339+
1340+
Ok(values)
1341+
}
1342+
}

0 commit comments

Comments
 (0)