Skip to content

Commit 1d1e5db

Browse files
committed
Add GcSimpleAlloc::alloc_array_from_vec
Fix miscelaneous clippy lints
1 parent ffd8187 commit 1d1e5db

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/array/repr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
//! 1. FatArrayRepr - Represents arrays as a fat pointer
66
//! 2. ThinArrayRepr - Represents arrays as a thin pointer,
77
//! with the length stored indirectly in the object header.
8+
#![allow(
9+
clippy::len_without_is_empty, // This is really an internal interface...
10+
)]
811
use core::marker::PhantomData;
912
use core::ptr::NonNull;
1013

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,36 @@ pub unsafe trait GcSimpleAlloc: GcContext {
511511
)
512512
}
513513
}
514+
/// Allocate an array, taking ownership of the values in
515+
/// the specified vec.
516+
#[inline]
517+
fn alloc_array_from_vec<'gc, T>(&'gc self, mut src: Vec<T>) -> GcArray<'gc, T, Self::Id>
518+
where T: GcSafe<'gc, Self::Id> + 'gc {
519+
unsafe {
520+
let ptr = src.as_ptr();
521+
let len = src.len();
522+
/*
523+
* NOTE: Don't steal ownership of the
524+
* source Vec until *after* we allocate.
525+
*
526+
* It is possible allocation panics in
527+
* which case we want to free the source elements.
528+
*/
529+
let (_, dest) = self.alloc_uninit_slice::<T>(len);
530+
/*
531+
* NOTE: From here to the end,
532+
* we should be infallible.
533+
*/
534+
src.set_len(0);
535+
dest.copy_from_nonoverlapping(ptr, len);
536+
let res = GcArray::from_raw_ptr(
537+
NonNull::new_unchecked(ptr as *mut _),
538+
len
539+
);
540+
drop(src);
541+
res
542+
}
543+
}
514544
/// Allocate a slice by filling it with results from the specified closure.
515545
///
516546
/// The closure receives the target index as its only argument.

0 commit comments

Comments
 (0)