Skip to content

Commit a5b9827

Browse files
committed
Add RawRc methods for MaybeUninit<T> values
1 parent 1d6d496 commit a5b9827

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

library/alloc/src/raw_rc/raw_rc.rs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use core::clone::CloneToUninit;
55
#[cfg(not(no_global_oom_handling))]
66
use core::convert;
77
use core::marker::PhantomData;
8-
use core::mem::DropGuard;
98
#[cfg(not(no_global_oom_handling))]
10-
use core::mem::{self, MaybeUninit, SizedTypeProperties};
9+
use core::mem::{self, SizedTypeProperties};
10+
use core::mem::{DropGuard, MaybeUninit};
1111
#[cfg(not(no_global_oom_handling))]
1212
use core::ops::{DerefMut, Residual, Try};
1313
#[cfg(not(no_global_oom_handling))]
@@ -453,7 +453,7 @@ impl<T, A> RawRc<T, A> {
453453
unsafe {
454454
allocation.get_mut_unchecked().write(mapped_value);
455455

456-
allocation.cast()
456+
allocation.assume_init()
457457
}
458458
} else {
459459
// Destruct `self` if `f` panics or returns a failure value.
@@ -529,6 +529,72 @@ impl<T, A> RawRc<T, A> {
529529
}
530530
}
531531

532+
impl<T, A> RawRc<MaybeUninit<T>, A> {
533+
pub(crate) fn try_new_uninit() -> Result<Self, AllocError>
534+
where
535+
A: Allocator + Default,
536+
{
537+
RawWeak::try_new_uninit::<1>().map(|weak| unsafe { Self::from_weak(weak) })
538+
}
539+
540+
pub(crate) fn try_new_uninit_in(alloc: A) -> Result<Self, AllocError>
541+
where
542+
A: Allocator,
543+
{
544+
RawWeak::try_new_uninit_in::<1>(alloc).map(|weak| unsafe { Self::from_weak(weak) })
545+
}
546+
547+
pub(crate) fn try_new_zeroed() -> Result<Self, AllocError>
548+
where
549+
A: Allocator + Default,
550+
{
551+
RawWeak::try_new_zeroed::<1>().map(|weak| unsafe { Self::from_weak(weak) })
552+
}
553+
554+
pub(crate) fn try_new_zeroed_in(alloc: A) -> Result<Self, AllocError>
555+
where
556+
A: Allocator,
557+
{
558+
RawWeak::try_new_zeroed_in::<1>(alloc).map(|weak| unsafe { Self::from_weak(weak) })
559+
}
560+
561+
#[cfg(not(no_global_oom_handling))]
562+
pub(crate) fn new_uninit() -> Self
563+
where
564+
A: Allocator + Default,
565+
{
566+
unsafe { Self::from_weak(RawWeak::new_uninit::<1>()) }
567+
}
568+
569+
#[cfg(not(no_global_oom_handling))]
570+
pub(crate) fn new_uninit_in(alloc: A) -> Self
571+
where
572+
A: Allocator,
573+
{
574+
unsafe { Self::from_weak(RawWeak::new_uninit_in::<1>(alloc)) }
575+
}
576+
577+
#[cfg(not(no_global_oom_handling))]
578+
pub(crate) fn new_zeroed() -> Self
579+
where
580+
A: Allocator + Default,
581+
{
582+
unsafe { Self::from_weak(RawWeak::new_zeroed::<1>()) }
583+
}
584+
585+
#[cfg(not(no_global_oom_handling))]
586+
pub(crate) fn new_zeroed_in(alloc: A) -> Self
587+
where
588+
A: Allocator,
589+
{
590+
unsafe { Self::from_weak(RawWeak::new_zeroed_in::<1>(alloc)) }
591+
}
592+
593+
pub(crate) unsafe fn assume_init(self) -> RawRc<T, A> {
594+
unsafe { self.cast() }
595+
}
596+
}
597+
532598
/// Decrements strong reference count in a reference-counted allocation with a value object that is
533599
/// pointed to by `value_ptr`.
534600
#[inline]

0 commit comments

Comments
 (0)