Skip to content

Commit 8093e8d

Browse files
committed
FEAT: Switch to using MaybeUninit for everything
Use std::mem::MaybeUninit and stop using nodrop as a fallback. This means we require Rust 1.36
1 parent ba94336 commit 8093e8d

File tree

10 files changed

+23
-270
lines changed

10 files changed

+23
-270
lines changed

.travis.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ env:
44
- FEATURES='serde-1'
55
matrix:
66
include:
7-
- rust: 1.24.1
7+
- rust: 1.36.0
88
env:
99
- FEATURES='array-sizes-33-128 array-sizes-129-255'
1010
- rust: stable
@@ -14,23 +14,17 @@ matrix:
1414
- rust: stable
1515
env:
1616
- FEATURES='array-sizes-33-128 array-sizes-129-255'
17-
- ARRAYVECTEST_ENSURE_MAYBEUNINIT=1
1817
- rust: beta
1918
- rust: nightly
20-
env:
21-
- ARRAYVECTEST_ENSURE_UNION=1
2219
- rust: nightly
2320
env:
2421
- FEATURES='serde'
25-
- ARRAYVECTEST_ENSURE_UNION=1
2622
- rust: nightly
2723
env:
2824
- FEATURES='serde-1'
29-
- ARRAYVECTEST_ENSURE_UNION=1
3025
- rust: nightly
3126
env:
3227
- FEATURES='array-sizes-33-128 array-sizes-129-255'
33-
- ARRAYVECTEST_ENSURE_MAYBEUNINIT=1
3428
branches:
3529
only:
3630
- master

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ categories = ["data-structures", "no-std"]
1313

1414
[build-dependencies]
1515

16-
[dependencies]
17-
nodrop = { version = "0.1.12", path = "nodrop", default-features = false }
18-
1916
[dependencies.serde]
2017
version = "1.0"
2118
optional = true

build.rs

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/array_string.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use char::encode_utf8;
1717
#[cfg(feature="serde-1")]
1818
use serde::{Serialize, Deserialize, Serializer, Deserializer};
1919

20-
use super::MaybeUninitCopy;
20+
use super::MaybeUninit as MaybeUninitCopy;
2121

2222
/// A string with a fixed capacity.
2323
///
@@ -98,10 +98,10 @@ impl<A> ArrayString<A>
9898
/// ```
9999
pub fn from_byte_string(b: &A) -> Result<Self, Utf8Error> {
100100
let len = str::from_utf8(b.as_slice())?.len();
101-
debug_assert_eq!(len, A::capacity());
101+
debug_assert_eq!(len, A::CAPACITY);
102102
Ok(ArrayString {
103103
xs: MaybeUninitCopy::from(*b),
104-
len: Index::from(A::capacity()),
104+
len: Index::from(A::CAPACITY),
105105
})
106106
}
107107

@@ -114,7 +114,7 @@ impl<A> ArrayString<A>
114114
/// assert_eq!(string.capacity(), 3);
115115
/// ```
116116
#[inline]
117-
pub fn capacity(&self) -> usize { A::capacity() }
117+
pub fn capacity(&self) -> usize { A::CAPACITY }
118118

119119
/// Return if the `ArrayString` is completely filled.
120120
///

src/lib.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ extern crate serde;
2828
#[cfg(not(feature="std"))]
2929
extern crate core as std;
3030

31-
#[cfg(not(has_manually_drop_in_union))]
32-
extern crate nodrop;
33-
3431
use std::cmp;
3532
use std::iter;
3633
use std::mem;
@@ -50,19 +47,8 @@ use std::fmt;
5047
use std::io;
5148

5249

53-
#[cfg(has_stable_maybe_uninit)]
54-
#[path="maybe_uninit_stable.rs"]
55-
mod maybe_uninit;
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)))]
59-
#[path="maybe_uninit_nodrop.rs"]
6050
mod maybe_uninit;
61-
62-
mod maybe_uninit_copy;
63-
6451
use maybe_uninit::MaybeUninit;
65-
use maybe_uninit_copy::MaybeUninitCopy;
6652

6753
#[cfg(feature="serde-1")]
6854
use serde::{Serialize, Deserialize, Serializer, Deserializer};
@@ -223,7 +209,7 @@ impl<A: Array> ArrayVec<A> {
223209
/// assert!(overflow.is_err());
224210
/// ```
225211
pub fn try_push(&mut self, element: A::Item) -> Result<(), CapacityError<A::Item>> {
226-
if self.len() < A::capacity() {
212+
if self.len() < A::CAPACITY {
227213
unsafe {
228214
self.push_unchecked(element);
229215
}
@@ -258,7 +244,7 @@ impl<A: Array> ArrayVec<A> {
258244
#[inline]
259245
pub unsafe fn push_unchecked(&mut self, element: A::Item) {
260246
let len = self.len();
261-
debug_assert!(len < A::capacity());
247+
debug_assert!(len < A::CAPACITY);
262248
ptr::write(self.get_unchecked_mut(len), element);
263249
self.set_len(len + 1);
264250
}
@@ -680,7 +666,7 @@ impl<A: Array> DerefMut for ArrayVec<A> {
680666
/// ```
681667
impl<A: Array> From<A> for ArrayVec<A> {
682668
fn from(array: A) -> Self {
683-
ArrayVec { xs: MaybeUninit::from(array), len: Index::from(A::capacity()) }
669+
ArrayVec { xs: MaybeUninit::from(array), len: Index::from(A::CAPACITY) }
684670
}
685671
}
686672

src/maybe_uninit.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11

22

33
use array::Array;
4-
use std::mem::ManuallyDrop;
5-
6-
/// A combination of ManuallyDrop and “maybe uninitialized”;
7-
/// this wraps a value that can be wholly or partially uninitialized;
8-
/// it also has no drop regardless of the type of T.
9-
#[repr(C)] // for cast from self ptr to value
10-
pub union MaybeUninit<T> {
11-
empty: (),
12-
value: ManuallyDrop<T>,
4+
use std::mem::MaybeUninit as StdMaybeUninit;
5+
6+
#[derive(Copy)]
7+
pub struct MaybeUninit<T> {
8+
inner: StdMaybeUninit<T>,
9+
}
10+
11+
impl<T> Clone for MaybeUninit<T>
12+
where T: Copy
13+
{
14+
fn clone(&self) -> Self { *self }
1315
}
14-
// Why we don't use std's MaybeUninit on nightly? See the ptr method
1516

1617
impl<T> MaybeUninit<T> {
1718
/// Create a new MaybeUninit with uninitialized interior
1819
pub unsafe fn uninitialized() -> Self {
19-
MaybeUninit { empty: () }
20+
MaybeUninit { inner: StdMaybeUninit::uninit() }
2021
}
2122

2223
/// Create a new MaybeUninit from the value `v`.
2324
pub fn from(v: T) -> Self {
24-
MaybeUninit { value: ManuallyDrop::new(v) }
25+
MaybeUninit { inner: StdMaybeUninit::new(v) }
2526
}
2627

2728
// Raw pointer casts written so that we don't reference or access the
@@ -31,16 +32,13 @@ impl<T> MaybeUninit<T> {
3132
pub fn ptr(&self) -> *const T::Item
3233
where T: Array
3334
{
34-
// std MaybeUninit creates a &self.value reference here which is
35-
// not guaranteed to be sound in our case - we will partially
36-
// initialize the value, not always wholly.
37-
self as *const _ as *const T::Item
35+
self.inner.as_ptr() as *const T::Item
3836
}
3937

4038
/// Return a mut raw pointer to the start of the interior array
4139
pub fn ptr_mut(&mut self) -> *mut T::Item
4240
where T: Array
4341
{
44-
self as *mut _ as *mut T::Item
42+
self.inner.as_mut_ptr() as *mut T::Item
4543
}
4644
}

src/maybe_uninit_copy.rs

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/maybe_uninit_nodrop.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)