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

Add impls for Rc<[T]> and Arc<[T]> #552

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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