djin_protocol/types/
smart_ptr.rs

1use crate::{hint, Parcel, Settings, Error};
2
3use std::rc::Rc;
4use std::sync::Arc;
5use std::ops::Deref;
6use std::io::prelude::*;
7
8macro_rules! impl_smart_ptr_type {
9    ($ty:ident) => {
10        impl<T: Parcel> Parcel for $ty<T>
11        {
12            const TYPE_NAME: &'static str = stringify!($ty<T>);
13
14            fn read_field(read: &mut dyn Read,
15                          settings: &Settings,
16                          _: &mut hint::Hints) -> Result<Self, Error> {
17                let value = T::read(read, settings)?;
18                Ok($ty::new(value))
19            }
20
21            fn write_field(&self, write: &mut dyn Write,
22                           settings: &Settings,
23                           _: &mut hint::Hints) -> Result<(), Error> {
24                self.deref().write(write, settings)
25            }
26        }
27    }
28}
29
30impl_smart_ptr_type!(Rc);
31impl_smart_ptr_type!(Arc);
32