Skip to content

Commit

Permalink
Vendor MaybeUninit from libcore for old Rust versions
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Oct 4, 2019
1 parent d958ce1 commit a5d61df
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ fn main() {
if cfg.probe_rustc_version(1, 34) {
println!("cargo:rustc-cfg=has_sized_atomics");
}
if cfg.probe_rustc_version(1, 36) {
println!("cargo:rustc-cfg=has_maybe_uninit");
}
}
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
feature(thread_local, checked_duration_since)
)]

mod maybe_uninit;
mod parking_lot;
mod spinwait;
mod thread_parker;
Expand Down
41 changes: 41 additions & 0 deletions core/src/maybe_uninit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! This module contains a re-export or vendored version of `core::mem::MaybeUninit` depending
//! on which Rust version it's compiled for.
//!
//! Remove this module and use `core::mem::MaybeUninit` directly when dropping support for <1.36
#![allow(dead_code)]

#[cfg(has_maybe_uninit)]
pub use core::mem::MaybeUninit;

#[cfg(not(has_maybe_uninit))]
use core::mem::ManuallyDrop;

/// Copied from `core::mem::MaybeUninit` to support Rust older than 1.36
#[cfg(not(has_maybe_uninit))]
pub union MaybeUninit<T: Copy> {
uninit: (),
value: ManuallyDrop<T>,
}

#[cfg(not(has_maybe_uninit))]
impl<T: Copy> MaybeUninit<T> {
#[inline(always)]
pub fn uninit() -> MaybeUninit<T> {
MaybeUninit { uninit: () }
}

#[inline(always)]
pub fn as_ptr(&self) -> *const T {
unsafe { &*self.value as *const T }
}

#[inline(always)]
pub fn as_mut_ptr(&mut self) -> *mut T {
unsafe { &mut *self.value as *mut T }
}

#[inline(always)]
pub unsafe fn assume_init(self) -> T {
ManuallyDrop::into_inner(self.value)
}
}

0 comments on commit a5d61df

Please sign in to comment.