@@ -189,6 +189,7 @@ impl<T> Box<T> {
189189 #[ cfg( not( no_global_oom_handling) ) ]
190190 #[ inline( always) ]
191191 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
192+ #[ must_use]
192193 pub fn new ( x : T ) -> Self {
193194 box x
194195 }
@@ -213,6 +214,7 @@ impl<T> Box<T> {
213214 /// ```
214215 #[ cfg( not( no_global_oom_handling) ) ]
215216 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
217+ #[ must_use]
216218 #[ inline]
217219 pub fn new_uninit ( ) -> Box < mem:: MaybeUninit < T > > {
218220 Self :: new_uninit_in ( Global )
@@ -239,6 +241,7 @@ impl<T> Box<T> {
239241 #[ cfg( not( no_global_oom_handling) ) ]
240242 #[ inline]
241243 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
244+ #[ must_use]
242245 pub fn new_zeroed ( ) -> Box < mem:: MaybeUninit < T > > {
243246 Self :: new_zeroed_in ( Global )
244247 }
@@ -247,6 +250,7 @@ impl<T> Box<T> {
247250 /// `x` will be pinned in memory and unable to be moved.
248251 #[ cfg( not( no_global_oom_handling) ) ]
249252 #[ stable( feature = "pin" , since = "1.33.0" ) ]
253+ #[ must_use]
250254 #[ inline( always) ]
251255 pub fn pin ( x : T ) -> Pin < Box < T > > {
252256 ( box x) . into ( )
@@ -341,6 +345,7 @@ impl<T, A: Allocator> Box<T, A> {
341345 /// ```
342346 #[ cfg( not( no_global_oom_handling) ) ]
343347 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
348+ #[ must_use]
344349 #[ inline]
345350 pub fn new_in ( x : T , alloc : A ) -> Self {
346351 let mut boxed = Self :: new_uninit_in ( alloc) ;
@@ -397,6 +402,7 @@ impl<T, A: Allocator> Box<T, A> {
397402 /// ```
398403 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
399404 #[ cfg( not( no_global_oom_handling) ) ]
405+ #[ must_use]
400406 // #[unstable(feature = "new_uninit", issue = "63291")]
401407 pub fn new_uninit_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A > {
402408 let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
@@ -461,6 +467,7 @@ impl<T, A: Allocator> Box<T, A> {
461467 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
462468 #[ cfg( not( no_global_oom_handling) ) ]
463469 // #[unstable(feature = "new_uninit", issue = "63291")]
470+ #[ must_use]
464471 pub fn new_zeroed_in ( alloc : A ) -> Box < mem:: MaybeUninit < T > , A > {
465472 let layout = Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
466473 // NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -505,6 +512,7 @@ impl<T, A: Allocator> Box<T, A> {
505512 /// `x` will be pinned in memory and unable to be moved.
506513 #[ cfg( not( no_global_oom_handling) ) ]
507514 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
515+ #[ must_use]
508516 #[ inline( always) ]
509517 pub fn pin_in ( x : T , alloc : A ) -> Pin < Self >
510518 where
@@ -563,6 +571,7 @@ impl<T> Box<[T]> {
563571 /// ```
564572 #[ cfg( not( no_global_oom_handling) ) ]
565573 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
574+ #[ must_use]
566575 pub fn new_uninit_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
567576 unsafe { RawVec :: with_capacity ( len) . into_box ( len) }
568577 }
@@ -587,6 +596,7 @@ impl<T> Box<[T]> {
587596 /// [zeroed]: mem::MaybeUninit::zeroed
588597 #[ cfg( not( no_global_oom_handling) ) ]
589598 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
599+ #[ must_use]
590600 pub fn new_zeroed_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
591601 unsafe { RawVec :: with_capacity_zeroed ( len) . into_box ( len) }
592602 }
@@ -683,6 +693,7 @@ impl<T, A: Allocator> Box<[T], A> {
683693 #[ cfg( not( no_global_oom_handling) ) ]
684694 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
685695 // #[unstable(feature = "new_uninit", issue = "63291")]
696+ #[ must_use]
686697 pub fn new_uninit_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
687698 unsafe { RawVec :: with_capacity_in ( len, alloc) . into_box ( len) }
688699 }
@@ -710,6 +721,7 @@ impl<T, A: Allocator> Box<[T], A> {
710721 #[ cfg( not( no_global_oom_handling) ) ]
711722 #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
712723 // #[unstable(feature = "new_uninit", issue = "63291")]
724+ #[ must_use]
713725 pub fn new_zeroed_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
714726 unsafe { RawVec :: with_capacity_zeroed_in ( len, alloc) . into_box ( len) }
715727 }
@@ -1088,6 +1100,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {
10881100 }
10891101}
10901102
1103+ #[ cfg( not( no_global_oom_handling) ) ]
10911104#[ stable( feature = "rust1" , since = "1.0.0" ) ]
10921105impl < T : Default > Default for Box < T > {
10931106 /// Creates a `Box<T>`, with the `Default` value for T.
@@ -1278,6 +1291,7 @@ impl<T> From<T> for Box<T> {
12781291 /// from the stack into it.
12791292 ///
12801293 /// # Examples
1294+ ///
12811295 /// ```rust
12821296 /// let x = 5;
12831297 /// let boxed = Box::new(5);
@@ -1331,6 +1345,12 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
13311345#[ cfg( not( no_global_oom_handling) ) ]
13321346#[ stable( feature = "box_from_cow" , since = "1.45.0" ) ]
13331347impl < T : Copy > From < Cow < ' _ , [ T ] > > for Box < [ T ] > {
1348+ /// Converts a `Cow<'_, [T]>` into a `Box<[T]>`
1349+ ///
1350+ /// When `cow` is the `Cow::Borrowed` variant, this
1351+ /// conversion allocates on the heap and copies the
1352+ /// underlying slice. Otherwise, it will try to reuse the owned
1353+ /// `Vec`'s allocation.
13341354 #[ inline]
13351355 fn from ( cow : Cow < ' _ , [ T ] > ) -> Box < [ T ] > {
13361356 match cow {
@@ -1349,6 +1369,7 @@ impl From<&str> for Box<str> {
13491369 /// and performs a copy of `s`.
13501370 ///
13511371 /// # Examples
1372+ ///
13521373 /// ```rust
13531374 /// let boxed: Box<str> = Box::from("hello");
13541375 /// println!("{}", boxed);
@@ -1362,6 +1383,29 @@ impl From<&str> for Box<str> {
13621383#[ cfg( not( no_global_oom_handling) ) ]
13631384#[ stable( feature = "box_from_cow" , since = "1.45.0" ) ]
13641385impl From < Cow < ' _ , str > > for Box < str > {
1386+ /// Converts a `Cow<'_, str>` into a `Box<str>`
1387+ ///
1388+ /// When `cow` is the `Cow::Borrowed` variant, this
1389+ /// conversion allocates on the heap and copies the
1390+ /// underlying `str`. Otherwise, it will try to reuse the owned
1391+ /// `String`'s allocation.
1392+ ///
1393+ /// # Examples
1394+ ///
1395+ /// ```rust
1396+ /// use std::borrow::Cow;
1397+ ///
1398+ /// let unboxed = Cow::Borrowed("hello");
1399+ /// let boxed: Box<str> = Box::from(unboxed);
1400+ /// println!("{}", boxed);
1401+ /// ```
1402+ ///
1403+ /// ```rust
1404+ /// # use std::borrow::Cow;
1405+ /// let unboxed = Cow::Owned("hello".to_string());
1406+ /// let boxed: Box<str> = Box::from(unboxed);
1407+ /// println!("{}", boxed);
1408+ /// ```
13651409 #[ inline]
13661410 fn from ( cow : Cow < ' _ , str > ) -> Box < str > {
13671411 match cow {
@@ -1396,13 +1440,15 @@ impl<A: Allocator> From<Box<str, A>> for Box<[u8], A> {
13961440 }
13971441}
13981442
1443+ #[ cfg( not( no_global_oom_handling) ) ]
13991444#[ stable( feature = "box_from_array" , since = "1.45.0" ) ]
14001445impl < T , const N : usize > From < [ T ; N ] > for Box < [ T ] > {
14011446 /// Converts a `[T; N]` into a `Box<[T]>`
14021447 ///
14031448 /// This conversion moves the array to newly heap-allocated memory.
14041449 ///
14051450 /// # Examples
1451+ ///
14061452 /// ```rust
14071453 /// let boxed: Box<[u8]> = Box::from([4, 2]);
14081454 /// println!("{:?}", boxed);
@@ -1416,6 +1462,15 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
14161462impl < T , const N : usize > TryFrom < Box < [ T ] > > for Box < [ T ; N ] > {
14171463 type Error = Box < [ T ] > ;
14181464
1465+ /// Attempts to convert a `Box<[T]>` into a `Box<[T; N]>`.
1466+ ///
1467+ /// The conversion occurs in-place and does not require a
1468+ /// new memory allocation.
1469+ ///
1470+ /// # Errors
1471+ ///
1472+ /// Returns the old `Box<[T]>` in the `Err` variant if
1473+ /// `boxed_slice.len()` does not equal `N`.
14191474 fn try_from ( boxed_slice : Box < [ T ] > ) -> Result < Self , Self :: Error > {
14201475 if boxed_slice. len ( ) == N {
14211476 Ok ( unsafe { Box :: from_raw ( Box :: into_raw ( boxed_slice) as * mut [ T ; N ] ) } )
0 commit comments