1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#![allow(clippy::module_name_repetitions)]
use std::{cell::Cell, marker::PhantomData, rc::Rc};
use crate::TypeSize;
use super::{sealed::ShouldCountInner, Borrowed, Owned};
/// A wrapper around [`Rc`] to implement [`TypeSize`] by allowing you to decide if to count the inner T's size.
///
/// # Examples
/// ```
/// # use std::{cell::Cell, rc::Rc};
/// # use typesize::{TypeSize, ptr::{SizableRc, Owned, Borrowed}};
/// #
/// let rc = Rc::new(0);
/// let rc_borrow: SizableRc<u8, Borrowed> = rc.clone().into();
/// let rc_owner: SizableRc<u8, Owned> = rc.into();
///
/// // Just counts the pointer to the internal `RcBox`.
/// assert_eq!(rc_borrow.get_size(), 0_usize.get_size());
/// // Counts the pointer to the `RcBox`, plus the two Cells, and the value.
/// assert_eq!(rc_owner.get_size(), 0_usize.get_size() + (std::mem::size_of::<usize>() * 2) + 0_u8.get_size());
/// ```
pub struct SizableRc<T, SC: ShouldCountInner>(pub Rc<T>, PhantomData<SC>);
impl<T, SC: ShouldCountInner> From<Rc<T>> for SizableRc<T, SC> {
fn from(value: Rc<T>) -> Self {
SizableRc(value, PhantomData)
}
}
impl<T, SC: ShouldCountInner> std::ops::Deref for SizableRc<T, SC> {
type Target = Rc<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> TypeSize for SizableRc<T, Borrowed> {}
impl<T: TypeSize> TypeSize for SizableRc<T, Owned> {
fn extra_size(&self) -> usize {
T::get_size(&self.0) + (std::mem::size_of::<Cell<usize>>() * 2)
}
}