mirror of
https://github.com/servo/servo.git
synced 2025-08-01 11:40:30 +01:00
Add UniqueArc::new_uninit, UniqueArc::assume_init
MozReview-Commit-ID: DoNcSsKpRn
This commit is contained in:
parent
005320c08c
commit
00b3fb49c4
1 changed files with 29 additions and 1 deletions
|
@ -34,7 +34,7 @@ use nodrop::NoDrop;
|
||||||
#[cfg(feature = "servo")]
|
#[cfg(feature = "servo")]
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use stable_deref_trait::{CloneStableDeref, StableDeref};
|
use stable_deref_trait::{CloneStableDeref, StableDeref};
|
||||||
use std::alloc::Layout;
|
use std::alloc::{self, Layout};
|
||||||
use std::borrow;
|
use std::borrow;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
|
@ -134,6 +134,23 @@ impl<T> UniqueArc<T> {
|
||||||
UniqueArc(Arc::new(data))
|
UniqueArc(Arc::new(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Construct an uninitialized arc
|
||||||
|
#[inline]
|
||||||
|
pub fn new_uninit() -> UniqueArc<mem::MaybeUninit<T>> {
|
||||||
|
unsafe {
|
||||||
|
let layout = Layout::new::<ArcInner<mem::MaybeUninit<T>>>();
|
||||||
|
let ptr = alloc::alloc(layout);
|
||||||
|
let mut p = ptr::NonNull::new(ptr)
|
||||||
|
.unwrap_or_else(|| alloc::handle_alloc_error(layout))
|
||||||
|
.cast::<ArcInner<mem::MaybeUninit<T>>>();
|
||||||
|
ptr::write(&mut p.as_mut().count, atomic::AtomicUsize::new(1));
|
||||||
|
UniqueArc(Arc {
|
||||||
|
p,
|
||||||
|
phantom: PhantomData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
/// Convert to a shareable Arc<T> once we're done mutating it
|
/// Convert to a shareable Arc<T> once we're done mutating it
|
||||||
pub fn shareable(self) -> Arc<T> {
|
pub fn shareable(self) -> Arc<T> {
|
||||||
|
@ -141,6 +158,17 @@ impl<T> UniqueArc<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> UniqueArc<mem::MaybeUninit<T>> {
|
||||||
|
/// Convert to an initialized Arc.
|
||||||
|
#[inline]
|
||||||
|
pub unsafe fn assume_init(this: Self) -> UniqueArc<T> {
|
||||||
|
UniqueArc(Arc {
|
||||||
|
p: this.0.p.cast(),
|
||||||
|
phantom: PhantomData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> Deref for UniqueArc<T> {
|
impl<T> Deref for UniqueArc<T> {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
fn deref(&self) -> &T {
|
fn deref(&self) -> &T {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue