18
18
//! around the main interface.
19
19
//!
20
20
//! [wg-ref]: https://github.com/rust-lang/unsafe-code-guidelines/issues/77
21
- use core:: { fmt, mem, slice, ptr} ;
22
21
use core:: alloc:: Layout ;
23
22
use core:: marker:: PhantomData ;
23
+ use core:: { fmt, mem, ptr, slice} ;
24
24
25
25
use crate :: boxed:: Box ;
26
26
use unsize:: CoerciblePtr ;
@@ -56,7 +56,8 @@ use unsize::CoerciblePtr;
56
56
/// structure for completely arbitrary other types. Just note that there is no integrated mechanis
57
57
/// for calling `Drop`.
58
58
///
59
- /// ```
59
+ #[ cfg_attr( miri, doc = "```no_run" ) ] // This can not work under miri yet, it relies on align_offset
60
+ #[ cfg_attr( not( miri) , doc = "```" ) ]
60
61
/// use core::mem::MaybeUninit;
61
62
/// use without_alloc::Uninit;
62
63
///
@@ -65,13 +66,13 @@ use unsize::CoerciblePtr;
65
66
/// let uninit = Uninit::from_maybe_uninit(&mut alloc);
66
67
///
67
68
/// // Now use the first `u32` for a counter:
68
- /// let mut counter = uninit.cast().unwrap( );
69
+ /// let mut counter = uninit.cast().expect("valid align" );
69
70
/// let mut tail = counter.split_to_fit();
70
71
/// let counter: &mut u32 = counter.init(0);
71
72
///
72
73
/// // And some more for a few `u64`.
73
74
/// // Note that these are not trivially aligned, but `Uninit` does that for us.
74
- /// let mut values = tail.split_cast().unwrap( );
75
+ /// let mut values = tail.split_cast().expect("enough space for split" );
75
76
/// // No more use, so don't bother with `split_to_fit` and just `init`.
76
77
/// let values: &mut [u64; 2] = values.init([0xdead, 0xbeef]);
77
78
/// ```
@@ -143,7 +144,8 @@ impl Uninit<'_, ()> {
143
144
///
144
145
/// Return `Ok` if this is possible in-bounds and `Err` if it is not.
145
146
pub fn split_layout ( & mut self , layout : Layout ) -> Option < Self > {
146
- self . view . split_layout ( layout)
147
+ self . view
148
+ . split_layout ( layout)
147
149
. map ( Self :: from_presumed_mutable_view)
148
150
}
149
151
}
@@ -221,7 +223,8 @@ impl<'a, T> Uninit<'a, T> {
221
223
///
222
224
/// Return `Ok` if the location is in-bounds and `Err` if it is out of bounds.
223
225
pub fn split_at_byte ( & mut self , at : usize ) -> Option < Uninit < ' a , ( ) > > {
224
- self . view . split_at_byte ( at)
226
+ self . view
227
+ . split_at_byte ( at)
225
228
. map ( Uninit :: from_presumed_mutable_view)
226
229
}
227
230
@@ -235,7 +238,8 @@ impl<'a, T> Uninit<'a, T> {
235
238
///
236
239
/// [`split_to_fit`]: #method.split_to_fit
237
240
pub fn cast < U > ( self ) -> Result < Uninit < ' a , U > , Self > {
238
- self . view . cast ( )
241
+ self . view
242
+ . cast ( )
239
243
. map ( Uninit :: from_presumed_mutable_view)
240
244
. map_err ( Self :: from_presumed_mutable_view)
241
245
}
@@ -246,7 +250,8 @@ impl<'a, T> Uninit<'a, T> {
246
250
/// one `U` and `Err` if it is not. Note that the successful result points to unused remaining
247
251
/// memory behind where the instances can be placed.
248
252
pub fn cast_slice < U > ( self ) -> Result < Uninit < ' a , [ U ] > , Self > {
249
- self . view . cast_slice :: < U > ( )
253
+ self . view
254
+ . cast_slice :: < U > ( )
250
255
. map ( Uninit :: from_presumed_mutable_view)
251
256
. map_err ( Self :: from_presumed_mutable_view)
252
257
}
@@ -259,7 +264,6 @@ impl<'a, T> Uninit<'a, T> {
259
264
}
260
265
}
261
266
262
-
263
267
impl < ' a , T : ?Sized > Uninit < ' a , T > {
264
268
/// Acquires the underlying *mut pointer.
265
269
pub const fn as_ptr ( & self ) -> * mut T {
@@ -337,7 +341,7 @@ impl<'a, T> Uninit<'a, T> {
337
341
/// `Uninit`.
338
342
pub fn into_maybe_uninit ( self ) -> & ' a mut mem:: MaybeUninit < T > {
339
343
// SAFETY: MaybeUninit is a transparent wrapper and need not be initialized.
340
- unsafe { & mut * ( self . as_ptr ( ) as * mut mem:: MaybeUninit < T > ) }
344
+ unsafe { & mut * ( self . as_ptr ( ) as * mut mem:: MaybeUninit < T > ) }
341
345
}
342
346
343
347
/// Read a value from the uninit place without moving it.
@@ -405,8 +409,7 @@ impl<'a, T> Uninit<'a, [T]> {
405
409
///
406
410
/// This is the pointer equivalent of `slice::split_at`.
407
411
pub fn split_at ( & mut self , at : usize ) -> Option < Self > {
408
- self . view . split_at ( at)
409
- . map ( Self :: from_presumed_mutable_view)
412
+ self . view . split_at ( at) . map ( Self :: from_presumed_mutable_view)
410
413
}
411
414
412
415
/// Get the trailing bytes behind the slice.
@@ -456,7 +459,8 @@ impl<'a, T> Uninit<'a, [T]> {
456
459
// SAFETY: MaybeUninit is a transparent wrapper and need not be initialized.
457
460
slice:: from_raw_parts_mut (
458
461
self . as_begin_ptr ( ) as * mut mem:: MaybeUninit < T > ,
459
- self . capacity ( ) )
462
+ self . capacity ( ) ,
463
+ )
460
464
}
461
465
}
462
466
}
@@ -559,9 +563,10 @@ impl UninitView<'_, ()> {
559
563
///
560
564
/// [`Uninit::split_layout`]: ./struct.Uninit.html#method.split_layout
561
565
pub fn split_layout ( & mut self , layout : Layout ) -> Option < Self > {
562
- let align = self . ptr . as_ptr ( )
563
- . align_offset ( layout. align ( ) ) ;
564
- let aligned_len = self . len
566
+ let align = self . ptr . as_ptr ( ) . align_offset ( layout. align ( ) ) ;
567
+
568
+ let aligned_len = self
569
+ . len
565
570
. checked_sub ( align)
566
571
. and_then ( |len| len. checked_sub ( layout. size ( ) ) ) ;
567
572
@@ -607,7 +612,11 @@ impl<T> UninitView<'_, T> {
607
612
/// # Panics
608
613
/// This method panics when the type parameter is not a zero sized type.
609
614
pub fn invent_for_zst ( ) -> Self {
610
- assert_eq ! ( mem:: size_of:: <T >( ) , 0 , "Invented ZST uninit invoked with non-ZST" ) ;
615
+ assert_eq ! (
616
+ mem:: size_of:: <T >( ) ,
617
+ 0 ,
618
+ "Invented ZST uninit invoked with non-ZST"
619
+ ) ;
611
620
let dangling = ptr:: NonNull :: < T > :: dangling ( ) ;
612
621
// SAFETY: all bytes are within the allocation.
613
622
let raw = unsafe { UninitView :: from_memory ( dangling. cast ( ) , 0 ) } ;
@@ -659,7 +668,7 @@ impl<'a, T> UninitView<'a, T> {
659
668
let empty = Layout :: for_value :: < [ U ] > ( & [ ] ) ;
660
669
661
670
if !self . fits ( empty) {
662
- return Err ( self )
671
+ return Err ( self ) ;
663
672
}
664
673
665
674
Ok ( UninitView {
@@ -880,7 +889,8 @@ impl<'a, T> UninitView<'a, [T]> {
880
889
// SAFETY: MaybeUninit is a transparent wrapper and need not be initialized.
881
890
slice:: from_raw_parts (
882
891
self . as_begin_ptr ( ) as * const mem:: MaybeUninit < T > ,
883
- self . capacity ( ) )
892
+ self . capacity ( ) ,
893
+ )
884
894
}
885
895
}
886
896
}
@@ -930,21 +940,21 @@ impl<'a, T> From<&'a [mem::MaybeUninit<T>]> for UninitView<'a, [T]> {
930
940
}
931
941
932
942
impl < T : ?Sized > fmt:: Debug for Uninit < ' _ , T > {
933
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
934
- f. debug_tuple ( "Uninit" )
935
- . field ( & self . view . ptr )
936
- . field ( & self . view . len )
937
- . finish ( )
938
- }
943
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
944
+ f. debug_tuple ( "Uninit" )
945
+ . field ( & self . view . ptr )
946
+ . field ( & self . view . len )
947
+ . finish ( )
948
+ }
939
949
}
940
950
941
951
impl < T : ?Sized > fmt:: Debug for UninitView < ' _ , T > {
942
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
943
- f. debug_tuple ( "UninitView" )
944
- . field ( & self . ptr )
945
- . field ( & self . len )
946
- . finish ( )
947
- }
952
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
953
+ f. debug_tuple ( "UninitView" )
954
+ . field ( & self . ptr )
955
+ . field ( & self . len )
956
+ . finish ( )
957
+ }
948
958
}
949
959
950
960
impl < ' a , T > From < Uninit < ' a , T > > for UninitView < ' a , T > {
@@ -954,15 +964,15 @@ impl<'a, T> From<Uninit<'a, T>> for UninitView<'a, T> {
954
964
}
955
965
956
966
impl < T > Default for Uninit < ' _ , [ T ] > {
957
- fn default ( ) -> Self {
958
- Uninit :: empty ( )
959
- }
967
+ fn default ( ) -> Self {
968
+ Uninit :: empty ( )
969
+ }
960
970
}
961
971
962
972
impl < T > Default for UninitView < ' _ , [ T ] > {
963
- fn default ( ) -> Self {
964
- UninitView :: empty ( )
965
- }
973
+ fn default ( ) -> Self {
974
+ UninitView :: empty ( )
975
+ }
966
976
}
967
977
968
978
impl < T : ?Sized > Clone for UninitView < ' _ , T > {
@@ -971,7 +981,7 @@ impl<T: ?Sized> Clone for UninitView<'_, T> {
971
981
}
972
982
}
973
983
974
- impl < T : ?Sized > Copy for UninitView < ' _ , T > { }
984
+ impl < T : ?Sized > Copy for UninitView < ' _ , T > { }
975
985
976
986
unsafe impl < ' a , T , U : ?Sized > CoerciblePtr < U > for UninitView < ' a , T > {
977
987
type Pointee = T ;
@@ -1011,12 +1021,12 @@ mod tests {
1011
1021
1012
1022
#[ test]
1013
1023
fn lifetime_longer ( ) {
1014
- fn _long < ' a , T > ( _: Uninit < ' a , & ' static T > ) { }
1024
+ fn _long < ' a , T > ( _: Uninit < ' a , & ' static T > ) { }
1015
1025
}
1016
1026
1017
1027
#[ test]
1018
1028
fn lifetime_shorter ( ) {
1019
- fn _short < ' a , T > ( _: Uninit < ' static , & ' a T > ) { }
1029
+ fn _short < ' a , T > ( _: Uninit < ' static , & ' a T > ) { }
1020
1030
}
1021
1031
1022
1032
#[ test]
0 commit comments