Skip to content

Commit 81e12ce

Browse files
committed
Add RawWeak methods for slice values
1 parent a5931bc commit 81e12ce

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

library/alloc/src/raw_rc/raw_weak.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,68 @@ impl<T, A> RawWeak<T, A> {
373373
}
374374
}
375375

376+
impl<T, A> RawWeak<[T], A> {
377+
#[cfg(not(no_global_oom_handling))]
378+
fn allocate_in<F>(length: usize, alloc: A, allocate_fn: F) -> Self
379+
where
380+
A: Allocator,
381+
F: FnOnce(&A, RcLayout) -> RcValuePointer,
382+
{
383+
let rc_layout = RcLayout::new_array::<T>(length);
384+
let ptr = allocate_fn(&alloc, rc_layout);
385+
386+
unsafe {
387+
Self::from_raw_parts(NonNull::slice_from_raw_parts(ptr.as_ptr().cast(), length), alloc)
388+
}
389+
}
390+
391+
#[cfg(not(no_global_oom_handling))]
392+
fn allocate<F>(length: usize, allocate_fn: F) -> Self
393+
where
394+
A: Allocator,
395+
F: FnOnce(RcLayout) -> (RcValuePointer, A),
396+
{
397+
let rc_layout = RcLayout::new_array::<T>(length);
398+
let (ptr, alloc) = allocate_fn(rc_layout);
399+
400+
unsafe {
401+
Self::from_raw_parts(NonNull::slice_from_raw_parts(ptr.as_ptr().cast(), length), alloc)
402+
}
403+
}
404+
405+
#[cfg(not(no_global_oom_handling))]
406+
pub(crate) fn new_uninit_slice_in<const STRONG_COUNT: usize>(length: usize, alloc: A) -> Self
407+
where
408+
A: Allocator,
409+
{
410+
Self::allocate_in(length, alloc, rc_alloc::allocate_uninit_in::<A, STRONG_COUNT>)
411+
}
412+
413+
#[cfg(not(no_global_oom_handling))]
414+
pub(crate) fn new_uninit_slice<const STRONG_COUNT: usize>(length: usize) -> Self
415+
where
416+
A: Allocator + Default,
417+
{
418+
Self::allocate(length, rc_alloc::allocate_uninit::<A, STRONG_COUNT>)
419+
}
420+
421+
#[cfg(not(no_global_oom_handling))]
422+
pub(crate) fn new_zeroed_slice_in<const STRONG_COUNT: usize>(length: usize, alloc: A) -> Self
423+
where
424+
A: Allocator,
425+
{
426+
Self::allocate_in(length, alloc, rc_alloc::allocate_zeroed_in::<A, STRONG_COUNT>)
427+
}
428+
429+
#[cfg(not(no_global_oom_handling))]
430+
pub(crate) fn new_zeroed_slice<const STRONG_COUNT: usize>(length: usize) -> Self
431+
where
432+
A: Allocator + Default,
433+
{
434+
Self::allocate(length, rc_alloc::allocate_zeroed::<A, STRONG_COUNT>)
435+
}
436+
}
437+
376438
// We choose `NonZeroUsize::MAX` as the address for dangling weak pointers because:
377439
//
378440
// - It does not point to any object that is stored inside a reference-counted allocation. Because

0 commit comments

Comments
 (0)