Skip to content

Commit d4efd56

Browse files
committed
Add support for Encode and Decode of Pin<Box<T>>
1 parent d0049d9 commit d4efd56

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/derive/impls.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use crate::derive::empty::EmptyCoder;
55
use crate::derive::map::{MapDecoder, MapEncoder};
66
use crate::derive::option::{OptionDecoder, OptionEncoder};
77
use crate::derive::result::{ResultDecoder, ResultEncoder};
8-
use crate::derive::smart_ptr::{DerefEncoder, FromDecoder};
8+
use crate::derive::smart_ptr::{DerefEncoder, FromDecoder, PinDecoder, PinEncoder};
9+
use core::ops::Deref;
910
use crate::derive::vec::{VecDecoder, VecEncoder};
1011
use crate::derive::{Decode, Encode};
1112
use crate::f32::{F32Decoder, F32Encoder};
1213
use crate::int::{CheckedIntDecoder, IntDecoder, IntEncoder};
1314
use crate::str::{StrDecoder, StrEncoder};
1415
use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
16+
use core::pin::Pin;
1517
use alloc::string::String;
1618
use alloc::vec::Vec;
1719
use core::marker::PhantomData;
@@ -111,6 +113,19 @@ impl_smart_ptr!(::alloc::rc::Rc);
111113
#[cfg(target_has_atomic = "ptr")]
112114
impl_smart_ptr!(::alloc::sync::Arc);
113115

116+
impl<P: Deref + Encode> Encode for Pin<P>
117+
where
118+
P::Target: Encode,
119+
{
120+
type Encoder = PinEncoder<P>;
121+
}
122+
impl<'a, P: Deref + Decode<'a>> Decode<'a> for Pin<P>
123+
where
124+
Pin<P>: From<P>,
125+
{
126+
type Decoder = PinDecoder<'a, P>;
127+
}
128+
114129
impl<T: Encode, const N: usize> Encode for [T; N] {
115130
type Encoder = ArrayEncoder<T, N>;
116131
}

src/derive/smart_ptr.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ impl<'a, F: From<T>, T: Decode<'a>> Decoder<'a, F> for FromDecoder<'a, T> {
5252
}
5353
}
5454

55+
pub type PinEncoder<P> = DerefEncoder<<P as Deref>::Target>;
56+
pub type PinDecoder<'a, P> = FromDecoder<'a, P>;
57+
5558
#[cfg(test)]
5659
mod tests {
5760
use crate::{decode, encode};
5861
use alloc::boxed::Box;
5962
use alloc::string::ToString;
63+
use core::pin::Pin;
6064

6165
#[test]
6266
fn box_() {
@@ -75,4 +79,10 @@ mod tests {
7579
let v = "box".to_string().into_boxed_str();
7680
assert_eq!(decode::<Box<str>>(&encode(&v)).unwrap(), v);
7781
}
82+
83+
#[test]
84+
fn pin_box_i32() {
85+
let v = Box::pin(7i32);
86+
assert_eq!(decode::<Pin<Box<i32>>>(&encode(&v)).unwrap(), v);
87+
}
7888
}

0 commit comments

Comments
 (0)