Skip to content

Commit

Permalink
Merge pull request #104 from tguichaoua/arc_unique_serde
Browse files Browse the repository at this point in the history
implement `Serialize` and `Deserialize` for `UniqueArc`
  • Loading branch information
Manishearth authored Dec 19, 2024
2 parents 3c3d47a + 9a9b2e1 commit fae2334
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/unique_arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use core::ops::{Deref, DerefMut};
use core::ptr::{self, addr_of_mut, NonNull};
use core::sync::atomic::AtomicUsize;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::iterator_as_exact_size_iterator::IteratorAsExactSizeIterator;
use crate::HeaderSlice;

Expand Down Expand Up @@ -274,6 +277,26 @@ unsafe impl<T, U: ?Sized> unsize::CoerciblePtr<U> for UniqueArc<T> {
}
}

#[cfg(feature = "serde")]
impl<'de, T: Deserialize<'de>> Deserialize<'de> for UniqueArc<T> {
fn deserialize<D>(deserializer: D) -> Result<UniqueArc<T>, D::Error>
where
D: ::serde::de::Deserializer<'de>,
{
T::deserialize(deserializer).map(UniqueArc::new)
}
}

#[cfg(feature = "serde")]
impl<T: Serialize> Serialize for UniqueArc<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::ser::Serializer,
{
(**self).serialize(serializer)
}
}

#[cfg(test)]
mod tests {
use crate::{Arc, HeaderSliceWithLength, HeaderWithLength, UniqueArc};
Expand Down

0 comments on commit fae2334

Please sign in to comment.