Skip to content

Commit c155b40

Browse files
authored
Merge pull request #127 from bluss/stable-maybe-uninit
Use stable MaybeUninit when we can (feature detected)
2 parents 21661fa + 93220e5 commit c155b40

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ matrix:
1515
- rust: stable
1616
env:
1717
- FEATURES='array-sizes-33-128 array-sizes-129-255'
18+
- ARRAYVECTEST_ENSURE_MAYBEUNINIT=1
1819
- rust: beta
1920
- rust: nightly
2021
env:
@@ -23,12 +24,12 @@ matrix:
2324
- rust: nightly
2425
env:
2526
- NODROP_FEATURES='use_needs_drop'
26-
- ARRAYVECTEST_ENSURE_UNION=1
27+
- ARRAYVECTEST_ENSURE_MAYBEUNINIT=1
2728
- rust: nightly
2829
env:
2930
- FEATURES='serde use_union'
3031
- NODROP_FEATURES='use_union'
31-
- ARRAYVECTEST_ENSURE_UNION=1
32+
- ARRAYVECTEST_ENSURE_MAYBEUNINIT=1
3233
branches:
3334
only:
3435
- master

build.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,28 @@ fn main() {
1111
}
1212

1313
fn detect_maybe_uninit() {
14+
let has_stable_maybe_uninit = probe(&stable_maybe_uninit());
15+
if has_stable_maybe_uninit {
16+
println!("cargo:rustc-cfg=has_stable_maybe_uninit");
17+
return;
18+
}
1419
let has_unstable_union_with_md = probe(&maybe_uninit_code(true));
1520
if has_unstable_union_with_md {
1621
println!("cargo:rustc-cfg=has_manually_drop_in_union");
1722
println!("cargo:rustc-cfg=has_union_feature");
18-
return;
1923
}
24+
}
2025

21-
let has_stable_union_with_md = probe(&maybe_uninit_code(false));
22-
if has_stable_union_with_md {
23-
println!("cargo:rustc-cfg=has_manually_drop_in_union");
24-
}
26+
// To guard against changes in this currently unstable feature, use
27+
// a detection tests instead of a Rustc version and/or date test.
28+
fn stable_maybe_uninit() -> String {
29+
let code = "
30+
#![allow(warnings)]
31+
use std::mem::MaybeUninit;
32+
33+
fn main() { }
34+
";
35+
code.to_string()
2536
}
2637

2738
// To guard against changes in this currently unstable feature, use

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ use std::fmt;
5050
use std::io;
5151

5252

53-
#[cfg(has_manually_drop_in_union)]
53+
#[cfg(has_stable_maybe_uninit)]
54+
#[path="maybe_uninit_stable.rs"]
5455
mod maybe_uninit;
55-
#[cfg(not(has_manually_drop_in_union))]
56+
#[cfg(all(not(has_stable_maybe_uninit), has_manually_drop_in_union))]
57+
mod maybe_uninit;
58+
#[cfg(all(not(has_stable_maybe_uninit), not(has_manually_drop_in_union)))]
5659
#[path="maybe_uninit_nodrop.rs"]
5760
mod maybe_uninit;
5861

src/maybe_uninit_stable.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
3+
use array::Array;
4+
use std::mem::MaybeUninit as StdMaybeUninit;
5+
6+
pub struct MaybeUninit<T> {
7+
inner: StdMaybeUninit<T>,
8+
}
9+
10+
impl<T> MaybeUninit<T> {
11+
/// Create a new MaybeUninit with uninitialized interior
12+
pub unsafe fn uninitialized() -> Self {
13+
MaybeUninit { inner: StdMaybeUninit::uninit() }
14+
}
15+
16+
/// Create a new MaybeUninit from the value `v`.
17+
pub fn from(v: T) -> Self {
18+
MaybeUninit { inner: StdMaybeUninit::new(v) }
19+
}
20+
21+
// Raw pointer casts written so that we don't reference or access the
22+
// uninitialized interior value
23+
24+
/// Return a raw pointer to the start of the interior array
25+
pub fn ptr(&self) -> *const T::Item
26+
where T: Array
27+
{
28+
// std MaybeUninit creates a &self.value reference here which is
29+
// not guaranteed to be sound in our case - we will partially
30+
// initialize the value, not always wholly.
31+
self.inner.as_ptr() as *const T::Item
32+
}
33+
34+
/// Return a mut raw pointer to the start of the interior array
35+
pub fn ptr_mut(&mut self) -> *mut T::Item
36+
where T: Array
37+
{
38+
self.inner.as_mut_ptr() as *mut T::Item
39+
}
40+
}

tests/tests.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,8 @@ fn test_sizes_129_255() {
510510

511511

512512
#[test]
513-
fn test_nightly_uses_maybe_uninit() {
514-
if option_env!("ARRAYVECTEST_ENSURE_UNION").map(|s| !s.is_empty()).unwrap_or(false) {
515-
assert!(cfg!(has_manually_drop_in_union));
516-
type ByteArray = ArrayVec<[u8; 4]>;
517-
assert!(mem::size_of::<ByteArray>() == 5);
513+
fn test_newish_stable_uses_maybe_uninit() {
514+
if option_env!("ARRAYVECTEST_ENSURE_MAYBEUNINIT").map(|s| !s.is_empty()).unwrap_or(false) {
515+
assert!(cfg!(has_stable_maybe_uninit));
518516
}
519517
}

0 commit comments

Comments
 (0)