mirror of https://git.sr.ht/~stygianentity/bincode
Added support for Cow, Rc and Arc
This commit is contained in:
parent
1d6379e715
commit
4e1a72796c
|
|
@ -5,7 +5,7 @@ use crate::{
|
||||||
error::{DecodeError, EncodeError},
|
error::{DecodeError, EncodeError},
|
||||||
Config,
|
Config,
|
||||||
};
|
};
|
||||||
use alloc::{boxed::Box, vec::Vec};
|
use alloc::{borrow::Cow, boxed::Box, rc::Rc, sync::Arc, vec::Vec};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct VecWriter {
|
struct VecWriter {
|
||||||
|
|
@ -35,7 +35,7 @@ pub fn encode_to_vec_with_config<E: enc::Encodeable, C: Config>(
|
||||||
Ok(encoder.into_writer().inner)
|
Ok(encoder.into_writer().inner)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, T> Decodable for Vec<T>
|
impl<T> Decodable for Vec<T>
|
||||||
where
|
where
|
||||||
T: Decodable,
|
T: Decodable,
|
||||||
{
|
{
|
||||||
|
|
@ -62,7 +62,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, T> Decodable for Box<T>
|
impl<T> Decodable for Box<T>
|
||||||
where
|
where
|
||||||
T: Decodable,
|
T: Decodable,
|
||||||
{
|
{
|
||||||
|
|
@ -81,7 +81,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de, T> Decodable for Box<[T]>
|
impl<T> Decodable for Box<[T]>
|
||||||
where
|
where
|
||||||
T: Decodable,
|
T: Decodable,
|
||||||
{
|
{
|
||||||
|
|
@ -103,3 +103,60 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'cow, T> Decodable for Cow<'cow, T>
|
||||||
|
where
|
||||||
|
T: Decodable + Clone,
|
||||||
|
{
|
||||||
|
fn decode<D: Decode>(decoder: D) -> Result<Self, DecodeError> {
|
||||||
|
let t = T::decode(decoder)?;
|
||||||
|
Ok(Cow::Owned(t))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'cow, T> Encodeable for Cow<'cow, T>
|
||||||
|
where
|
||||||
|
T: Encodeable + Clone,
|
||||||
|
{
|
||||||
|
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||||
|
self.as_ref().encode(encoder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Decodable for Rc<T>
|
||||||
|
where
|
||||||
|
T: Decodable,
|
||||||
|
{
|
||||||
|
fn decode<D: Decode>(decoder: D) -> Result<Self, DecodeError> {
|
||||||
|
let t = T::decode(decoder)?;
|
||||||
|
Ok(Rc::new(t))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Encodeable for Rc<T>
|
||||||
|
where
|
||||||
|
T: Encodeable,
|
||||||
|
{
|
||||||
|
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||||
|
T::encode(self, encoder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Decodable for Arc<T>
|
||||||
|
where
|
||||||
|
T: Decodable,
|
||||||
|
{
|
||||||
|
fn decode<D: Decode>(decoder: D) -> Result<Self, DecodeError> {
|
||||||
|
let t = T::decode(decoder)?;
|
||||||
|
Ok(Arc::new(t))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Encodeable for Arc<T>
|
||||||
|
where
|
||||||
|
T: Encodeable,
|
||||||
|
{
|
||||||
|
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||||
|
T::encode(self, encoder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,12 @@
|
||||||
#![cfg(feature = "alloc")]
|
#![cfg(feature = "alloc")]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
|
use alloc::borrow::Cow;
|
||||||
|
use alloc::rc::Rc;
|
||||||
|
use alloc::sync::Arc;
|
||||||
use utils::the_same;
|
use utils::the_same;
|
||||||
|
|
||||||
struct Foo {
|
struct Foo {
|
||||||
|
|
@ -44,4 +49,8 @@ fn test_alloc_commons() {
|
||||||
the_same::<Vec<u32>>(vec![1, 2, 3, 4, 5]);
|
the_same::<Vec<u32>>(vec![1, 2, 3, 4, 5]);
|
||||||
the_same(Box::<u32>::new(5));
|
the_same(Box::<u32>::new(5));
|
||||||
the_same(Box::<[u32]>::from(vec![1, 2, 3, 4, 5]));
|
the_same(Box::<[u32]>::from(vec![1, 2, 3, 4, 5]));
|
||||||
|
the_same(Cow::<u32>::Owned(5));
|
||||||
|
the_same(Cow::<u32>::Borrowed(&5));
|
||||||
|
the_same(Rc::<u32>::new(5));
|
||||||
|
the_same(Arc::<u32>::new(5));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue