1818//! around the main interface.
1919//!
2020//! [wg-ref]: https://github.com/rust-lang/unsafe-code-guidelines/issues/77
21- use core:: { fmt, mem, slice, ptr} ;
2221use core:: alloc:: Layout ;
2322use core:: marker:: PhantomData ;
23+ use core:: { fmt, mem, ptr, slice} ;
2424
2525use crate :: boxed:: Box ;
2626use unsize:: CoerciblePtr ;
@@ -56,7 +56,8 @@ use unsize::CoerciblePtr;
5656/// structure for completely arbitrary other types. Just note that there is no integrated mechanis
5757/// for calling `Drop`.
5858///
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 = "```" ) ]
6061/// use core::mem::MaybeUninit;
6162/// use without_alloc::Uninit;
6263///
@@ -65,13 +66,13 @@ use unsize::CoerciblePtr;
6566/// let uninit = Uninit::from_maybe_uninit(&mut alloc);
6667///
6768/// // Now use the first `u32` for a counter:
68- /// let mut counter = uninit.cast().unwrap( );
69+ /// let mut counter = uninit.cast().expect("valid align" );
6970/// let mut tail = counter.split_to_fit();
7071/// let counter: &mut u32 = counter.init(0);
7172///
7273/// // And some more for a few `u64`.
7374/// // 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" );
7576/// // No more use, so don't bother with `split_to_fit` and just `init`.
7677/// let values: &mut [u64; 2] = values.init([0xdead, 0xbeef]);
7778/// ```
@@ -143,7 +144,8 @@ impl Uninit<'_, ()> {
143144 ///
144145 /// Return `Ok` if this is possible in-bounds and `Err` if it is not.
145146 pub fn split_layout ( & mut self , layout : Layout ) -> Option < Self > {
146- self . view . split_layout ( layout)
147+ self . view
148+ . split_layout ( layout)
147149 . map ( Self :: from_presumed_mutable_view)
148150 }
149151}
@@ -221,7 +223,8 @@ impl<'a, T> Uninit<'a, T> {
221223 ///
222224 /// Return `Ok` if the location is in-bounds and `Err` if it is out of bounds.
223225 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)
225228 . map ( Uninit :: from_presumed_mutable_view)
226229 }
227230
@@ -235,7 +238,8 @@ impl<'a, T> Uninit<'a, T> {
235238 ///
236239 /// [`split_to_fit`]: #method.split_to_fit
237240 pub fn cast < U > ( self ) -> Result < Uninit < ' a , U > , Self > {
238- self . view . cast ( )
241+ self . view
242+ . cast ( )
239243 . map ( Uninit :: from_presumed_mutable_view)
240244 . map_err ( Self :: from_presumed_mutable_view)
241245 }
@@ -246,7 +250,8 @@ impl<'a, T> Uninit<'a, T> {
246250 /// one `U` and `Err` if it is not. Note that the successful result points to unused remaining
247251 /// memory behind where the instances can be placed.
248252 pub fn cast_slice < U > ( self ) -> Result < Uninit < ' a , [ U ] > , Self > {
249- self . view . cast_slice :: < U > ( )
253+ self . view
254+ . cast_slice :: < U > ( )
250255 . map ( Uninit :: from_presumed_mutable_view)
251256 . map_err ( Self :: from_presumed_mutable_view)
252257 }
@@ -259,7 +264,6 @@ impl<'a, T> Uninit<'a, T> {
259264 }
260265}
261266
262-
263267impl < ' a , T : ?Sized > Uninit < ' a , T > {
264268 /// Acquires the underlying *mut pointer.
265269 pub const fn as_ptr ( & self ) -> * mut T {
@@ -337,7 +341,7 @@ impl<'a, T> Uninit<'a, T> {
337341 /// `Uninit`.
338342 pub fn into_maybe_uninit ( self ) -> & ' a mut mem:: MaybeUninit < T > {
339343 // 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 > ) }
341345 }
342346
343347 /// Read a value from the uninit place without moving it.
@@ -405,8 +409,7 @@ impl<'a, T> Uninit<'a, [T]> {
405409 ///
406410 /// This is the pointer equivalent of `slice::split_at`.
407411 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)
410413 }
411414
412415 /// Get the trailing bytes behind the slice.
@@ -456,7 +459,8 @@ impl<'a, T> Uninit<'a, [T]> {
456459 // SAFETY: MaybeUninit is a transparent wrapper and need not be initialized.
457460 slice:: from_raw_parts_mut (
458461 self . as_begin_ptr ( ) as * mut mem:: MaybeUninit < T > ,
459- self . capacity ( ) )
462+ self . capacity ( ) ,
463+ )
460464 }
461465 }
462466}
@@ -559,9 +563,10 @@ impl UninitView<'_, ()> {
559563 ///
560564 /// [`Uninit::split_layout`]: ./struct.Uninit.html#method.split_layout
561565 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
565570 . checked_sub ( align)
566571 . and_then ( |len| len. checked_sub ( layout. size ( ) ) ) ;
567572
@@ -607,7 +612,11 @@ impl<T> UninitView<'_, T> {
607612 /// # Panics
608613 /// This method panics when the type parameter is not a zero sized type.
609614 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+ ) ;
611620 let dangling = ptr:: NonNull :: < T > :: dangling ( ) ;
612621 // SAFETY: all bytes are within the allocation.
613622 let raw = unsafe { UninitView :: from_memory ( dangling. cast ( ) , 0 ) } ;
@@ -659,7 +668,7 @@ impl<'a, T> UninitView<'a, T> {
659668 let empty = Layout :: for_value :: < [ U ] > ( & [ ] ) ;
660669
661670 if !self . fits ( empty) {
662- return Err ( self )
671+ return Err ( self ) ;
663672 }
664673
665674 Ok ( UninitView {
@@ -880,7 +889,8 @@ impl<'a, T> UninitView<'a, [T]> {
880889 // SAFETY: MaybeUninit is a transparent wrapper and need not be initialized.
881890 slice:: from_raw_parts (
882891 self . as_begin_ptr ( ) as * const mem:: MaybeUninit < T > ,
883- self . capacity ( ) )
892+ self . capacity ( ) ,
893+ )
884894 }
885895 }
886896}
@@ -930,21 +940,21 @@ impl<'a, T> From<&'a [mem::MaybeUninit<T>]> for UninitView<'a, [T]> {
930940}
931941
932942impl < 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+ }
939949}
940950
941951impl < 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+ }
948958}
949959
950960impl < ' 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> {
954964}
955965
956966impl < T > Default for Uninit < ' _ , [ T ] > {
957- fn default ( ) -> Self {
958- Uninit :: empty ( )
959- }
967+ fn default ( ) -> Self {
968+ Uninit :: empty ( )
969+ }
960970}
961971
962972impl < T > Default for UninitView < ' _ , [ T ] > {
963- fn default ( ) -> Self {
964- UninitView :: empty ( )
965- }
973+ fn default ( ) -> Self {
974+ UninitView :: empty ( )
975+ }
966976}
967977
968978impl < T : ?Sized > Clone for UninitView < ' _ , T > {
@@ -971,7 +981,7 @@ impl<T: ?Sized> Clone for UninitView<'_, T> {
971981 }
972982}
973983
974- impl < T : ?Sized > Copy for UninitView < ' _ , T > { }
984+ impl < T : ?Sized > Copy for UninitView < ' _ , T > { }
975985
976986unsafe impl < ' a , T , U : ?Sized > CoerciblePtr < U > for UninitView < ' a , T > {
977987 type Pointee = T ;
@@ -1011,12 +1021,12 @@ mod tests {
10111021
10121022 #[ test]
10131023 fn lifetime_longer ( ) {
1014- fn _long < ' a , T > ( _: Uninit < ' a , & ' static T > ) { }
1024+ fn _long < ' a , T > ( _: Uninit < ' a , & ' static T > ) { }
10151025 }
10161026
10171027 #[ test]
10181028 fn lifetime_shorter ( ) {
1019- fn _short < ' a , T > ( _: Uninit < ' static , & ' a T > ) { }
1029+ fn _short < ' a , T > ( _: Uninit < ' static , & ' a T > ) { }
10201030 }
10211031
10221032 #[ test]
0 commit comments