Skip to content

Commit

Permalink
Optional support for borsh serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuuzetsu authored and bluss committed Mar 7, 2024
1 parent 7a1722e commit 4337b1b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Cargo.toml
Expand Up @@ -14,6 +14,11 @@ categories = ["data-structures", "no-std"]

[build-dependencies]

[dependencies.borsh]
version = "1.2.0"
optional = true
default-features = false

[dependencies.serde]
version = "1.0"
optional = true
Expand Down Expand Up @@ -49,7 +54,7 @@ debug = true
debug = true

[package.metadata.docs.rs]
features = ["serde", "zeroize"]
features = ["borsh", "serde", "zeroize"]

[package.metadata.release]
no-dev-version = true
Expand Down
22 changes: 22 additions & 0 deletions src/array_string.rs
Expand Up @@ -628,6 +628,28 @@ impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
}
}

#[cfg(feature = "borsh")]
/// Requires crate feature `"borsh"`
impl<const CAP: usize> borsh::BorshSerialize for ArrayString<CAP> {
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
<str as borsh::BorshSerialize>::serialize(&*self, writer)
}
}

#[cfg(feature = "borsh")]
/// Requires crate feature `"borsh"`
impl<const CAP: usize> borsh::BorshDeserialize for ArrayString<CAP> {
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
let s = <String as borsh::BorshDeserialize>::deserialize_reader(reader)?;
ArrayString::from(&s).map_err(|_| {
borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
format!("expected a string no more than {} bytes long", CAP),
)
})
}
}

impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
{
type Error = CapacityError<&'a str>;
Expand Down
39 changes: 39 additions & 0 deletions src/arrayvec.rs
Expand Up @@ -1301,3 +1301,42 @@ impl<'de, T: Deserialize<'de>, const CAP: usize> Deserialize<'de> for ArrayVec<T
deserializer.deserialize_seq(ArrayVecVisitor::<T, CAP>(PhantomData))
}
}

#[cfg(feature = "borsh")]
/// Requires crate feature `"borsh"`
impl<T, const CAP: usize> borsh::BorshSerialize for ArrayVec<T, CAP>
where
T: borsh::BorshSerialize,
{
fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
let vs = self.as_slice();
<usize as borsh::BorshSerialize>::serialize(&vs.len(), writer)?;
for elem in vs {
<T as borsh::BorshSerialize>::serialize(elem, writer)?;
}
Ok(())
}
}

#[cfg(feature = "borsh")]
/// Requires crate feature `"borsh"`
impl<T, const CAP: usize> borsh::BorshDeserialize for ArrayVec<T, CAP>
where
T: borsh::BorshDeserialize,
{
fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
let mut values = Self::new();
let len = <usize as borsh::BorshDeserialize>::deserialize_reader(reader)?;
for _ in 0..len {
let elem = <T as borsh::BorshDeserialize>::deserialize_reader(reader)?;
if let Err(_) = values.try_push(elem) {
return Err(borsh::io::Error::new(
borsh::io::ErrorKind::InvalidData,
format!("expected an array with no more than {} items", CAP),
));
}
}

Ok(values)
}
}

0 comments on commit 4337b1b

Please sign in to comment.