use super::Bump;
use alloc::rc;
use core::ops::Deref;
#[cfg(any(feature = "allocator_api", feature = "allocator-fallback"))]
use {
super::{AllocError, Allocator},
alloc::alloc::Layout,
core::ptr::NonNull,
};
pub struct Rc<Bump>(pub rc::Rc<Bump>);
impl<Bump> Rc<Bump> {
pub fn new(bump: Bump) -> Self {
Self(rc::Rc::new(bump))
}
}
impl<Bump: Default> Default for Rc<Bump> {
fn default() -> Self {
Self::new(Bump::default())
}
}
impl<Bump> Clone for Rc<Bump> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<Bump> Deref for Rc<Bump> {
type Target = Bump;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(any(feature = "allocator_api", feature = "allocator-fallback"))]
#[cfg_attr(
feature = "doc_cfg",
doc(cfg(any(
feature = "allocator_api",
feature = "allocator-fallback",
)))
)]
unsafe impl<Bump: Allocator> Allocator for Rc<Bump> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
Allocator::allocate(&*self.0, layout)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { Allocator::deallocate(&*self.0, ptr, layout) };
}
}
#[doc(hidden)]
#[deprecated = "use `fixed_bump::Rc<Bump<...>>` instead"]
pub struct RcBump<Size, Align = Size>(pub rc::Rc<Bump<Size, Align>>);
#[allow(deprecated)]
impl<Size, Align> RcBump<Size, Align> {
pub fn new() -> Self {
Self(rc::Rc::new(Bump::new()))
}
}
#[allow(deprecated)]
impl<Size, Align> Default for RcBump<Size, Align> {
fn default() -> Self {
Self::new()
}
}
#[allow(deprecated)]
impl<Size, Align> Clone for RcBump<Size, Align> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
#[allow(deprecated)]
impl<Size, Align> Deref for RcBump<Size, Align> {
type Target = Bump<Size, Align>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(any(feature = "allocator_api", feature = "allocator-fallback"))]
#[allow(deprecated)]
unsafe impl<Size, Align> Allocator for RcBump<Size, Align> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
Allocator::allocate(&*self.0, layout)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { Allocator::deallocate(&*self.0, ptr, layout) };
}
}