Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: BorshSerialize/BoshDeserialize/BorshSchema for Cell/RefCell #265

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions borsh/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,47 @@ impl<T: ?Sized> BorshDeserialize for PhantomData<T> {
Ok(PhantomData)
}
}

#[cfg(feature = "std")]
impl<T> BorshDeserialize for core::cell::Cell<T>
where
T: BorshDeserialize,
{
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
<T as BorshDeserialize>::deserialize_reader(reader).map(core::cell::Cell::new)
}
}

#[cfg(feature = "std")]
impl<T> BorshDeserialize for core::cell::RefCell<T>
where
T: BorshDeserialize,
{
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
<T as BorshDeserialize>::deserialize_reader(reader).map(core::cell::RefCell::new)
}
}

#[cfg(feature = "std")]
impl<T> BorshDeserialize for std::sync::Mutex<T>
where
T: BorshDeserialize,
{
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
<T as BorshDeserialize>::deserialize_reader(reader).map(std::sync::Mutex::new)
}
}

#[cfg(feature = "std")]
impl<T> BorshDeserialize for std::sync::RwLock<T>
where
T: BorshDeserialize,
{
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
<T as BorshDeserialize>::deserialize_reader(reader).map(std::sync::RwLock::new)
}
}

/// Deserializes an object from a slice of bytes.
/// # Example
/// ```
Expand Down
52 changes: 52 additions & 0 deletions borsh/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,55 @@ impl<T: ?Sized> BorshSerialize for PhantomData<T> {
Ok(())
}
}

#[cfg(feature = "std")]
impl<T> BorshSerialize for core::cell::Cell<T>
where
T: BorshSerialize + Copy,
{
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
<T as BorshSerialize>::serialize(&self.get(), writer)
}
}

#[cfg(feature = "std")]
impl<T> BorshSerialize for core::cell::RefCell<T>
where
T: BorshSerialize,
{
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
<T as BorshSerialize>::serialize(core::ops::Deref::deref(&self.borrow()), writer)
}
}

#[cfg(feature = "std")]
impl<T> BorshSerialize for std::sync::Mutex<T>
where
T: BorshSerialize,
{
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
match self.lock().as_deref() {
Ok(locked) => <T as BorshSerialize>::serialize(locked, writer),
Err(_) => Err(Error::new(
ErrorKind::InvalidData,
"lock poison error while serializing",
)),
}
}
}

#[cfg(feature = "std")]
impl<T> BorshSerialize for std::sync::RwLock<T>
where
T: BorshSerialize,
{
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
match self.read().as_deref() {
Ok(locked) => <T as BorshSerialize>::serialize(locked, writer),
Err(_) => Err(Error::new(
ErrorKind::InvalidData,
"lock poison error while serializing",
)),
}
}
}
Loading