File tree 5 files changed +67
-14
lines changed
5 files changed +67
-14
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ matrix:
15
15
- rust : stable
16
16
env :
17
17
- FEATURES='array-sizes-33-128 array-sizes-129-255'
18
+ - ARRAYVECTEST_ENSURE_MAYBEUNINIT=1
18
19
- rust : beta
19
20
- rust : nightly
20
21
env :
@@ -23,12 +24,12 @@ matrix:
23
24
- rust : nightly
24
25
env :
25
26
- NODROP_FEATURES='use_needs_drop'
26
- - ARRAYVECTEST_ENSURE_UNION =1
27
+ - ARRAYVECTEST_ENSURE_MAYBEUNINIT =1
27
28
- rust : nightly
28
29
env :
29
30
- FEATURES='serde use_union'
30
31
- NODROP_FEATURES='use_union'
31
- - ARRAYVECTEST_ENSURE_UNION =1
32
+ - ARRAYVECTEST_ENSURE_MAYBEUNINIT =1
32
33
branches :
33
34
only :
34
35
- master
Original file line number Diff line number Diff line change @@ -11,17 +11,28 @@ fn main() {
11
11
}
12
12
13
13
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
+ }
14
19
let has_unstable_union_with_md = probe ( & maybe_uninit_code ( true ) ) ;
15
20
if has_unstable_union_with_md {
16
21
println ! ( "cargo:rustc-cfg=has_manually_drop_in_union" ) ;
17
22
println ! ( "cargo:rustc-cfg=has_union_feature" ) ;
18
- return ;
19
23
}
24
+ }
20
25
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 ( )
25
36
}
26
37
27
38
// To guard against changes in this currently unstable feature, use
Original file line number Diff line number Diff line change @@ -50,9 +50,12 @@ use std::fmt;
50
50
use std:: io;
51
51
52
52
53
- #[ cfg( has_manually_drop_in_union) ]
53
+ #[ cfg( has_stable_maybe_uninit) ]
54
+ #[ path="maybe_uninit_stable.rs" ]
54
55
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) ) ) ]
56
59
#[ path="maybe_uninit_nodrop.rs" ]
57
60
mod maybe_uninit;
58
61
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -510,10 +510,8 @@ fn test_sizes_129_255() {
510
510
511
511
512
512
#[ 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) ) ;
518
516
}
519
517
}
You can’t perform that action at this time.
0 commit comments