Skip to content

Commit

Permalink
Add impls for Rc<[T]> and Arc<[T]> (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejhirsz authored Jun 15, 2022
1 parent e8ccb0a commit 18d5249
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/features/impl_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,26 @@ where
}
}

impl<T> Decode for Rc<[T]>
where
T: Decode,
{
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
let vec = Vec::decode(decoder)?;
Ok(vec.into())
}
}

impl<'de, T> BorrowDecode<'de> for Rc<[T]>
where
T: BorrowDecode<'de> + 'de,
{
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, DecodeError> {
let vec = Vec::borrow_decode(decoder)?;
Ok(vec.into())
}
}

#[cfg(target_has_atomic = "ptr")]
impl<T> Decode for Arc<T>
where
Expand Down Expand Up @@ -482,3 +502,25 @@ where
T::encode(self, encoder)
}
}

#[cfg(target_has_atomic = "ptr")]
impl<T> Decode for Arc<[T]>
where
T: Decode,
{
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
let vec = Vec::decode(decoder)?;
Ok(vec.into())
}
}

#[cfg(target_has_atomic = "ptr")]
impl<'de, T> BorrowDecode<'de> for Arc<[T]>
where
T: BorrowDecode<'de> + 'de,
{
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, DecodeError> {
let vec = Vec::borrow_decode(decoder)?;
Ok(vec.into())
}
}
3 changes: 3 additions & 0 deletions tests/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ fn test_alloc_commons() {
{
// Serde doesn't support Rc or Arc
the_same(Rc::<u32>::new(5));
the_same(Rc::<[u32]>::from(vec![1, 2, 3, 4, 5]));

#[cfg(target_has_atomic = "ptr")]
{
the_same(Arc::<u32>::new(5));
the_same(Arc::<[u32]>::from(vec![1, 2, 3, 4, 5]));
}
}

Expand Down

0 comments on commit 18d5249

Please sign in to comment.