Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: alloc: add `Vec::try_with_capacity{,_in}()` constructors

Add `Vec::try_with_capacity()` and `Vec::try_with_capacity_in()` as
the fallible versions of `Vec::with_capacity()` and
`Vec::with_capacity_in()`, respectively.

The implementations follow the originals and use the previously
added `RawVec::try_with_capacity_in()`.

In turn, `Vec::try_with_capacity()` will be used to implement
the `CString` type (which wraps a `Vec<u8>`) in a later patch.

Reviewed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

+89 -1
-1
rust/alloc/raw_vec.rs
··· 135 135 136 136 /// Like `try_with_capacity`, but parameterized over the choice of 137 137 /// allocator for the returned `RawVec`. 138 - #[allow(dead_code)] 139 138 #[inline] 140 139 pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> { 141 140 Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc)
+89
rust/alloc/vec/mod.rs
··· 472 472 Self::with_capacity_in(capacity, Global) 473 473 } 474 474 475 + /// Tries to construct a new, empty `Vec<T>` with the specified capacity. 476 + /// 477 + /// The vector will be able to hold exactly `capacity` elements without 478 + /// reallocating. If `capacity` is 0, the vector will not allocate. 479 + /// 480 + /// It is important to note that although the returned vector has the 481 + /// *capacity* specified, the vector will have a zero *length*. For an 482 + /// explanation of the difference between length and capacity, see 483 + /// *[Capacity and reallocation]*. 484 + /// 485 + /// [Capacity and reallocation]: #capacity-and-reallocation 486 + /// 487 + /// # Examples 488 + /// 489 + /// ``` 490 + /// let mut vec = Vec::try_with_capacity(10).unwrap(); 491 + /// 492 + /// // The vector contains no items, even though it has capacity for more 493 + /// assert_eq!(vec.len(), 0); 494 + /// assert_eq!(vec.capacity(), 10); 495 + /// 496 + /// // These are all done without reallocating... 497 + /// for i in 0..10 { 498 + /// vec.push(i); 499 + /// } 500 + /// assert_eq!(vec.len(), 10); 501 + /// assert_eq!(vec.capacity(), 10); 502 + /// 503 + /// // ...but this may make the vector reallocate 504 + /// vec.push(11); 505 + /// assert_eq!(vec.len(), 11); 506 + /// assert!(vec.capacity() >= 11); 507 + /// 508 + /// let mut result = Vec::try_with_capacity(usize::MAX); 509 + /// assert!(result.is_err()); 510 + /// ``` 511 + #[inline] 512 + #[stable(feature = "kernel", since = "1.0.0")] 513 + pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> { 514 + Self::try_with_capacity_in(capacity, Global) 515 + } 516 + 475 517 /// Creates a `Vec<T>` directly from the raw components of another vector. 476 518 /// 477 519 /// # Safety ··· 657 615 #[unstable(feature = "allocator_api", issue = "32838")] 658 616 pub fn with_capacity_in(capacity: usize, alloc: A) -> Self { 659 617 Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 } 618 + } 619 + 620 + /// Tries to construct a new, empty `Vec<T, A>` with the specified capacity 621 + /// with the provided allocator. 622 + /// 623 + /// The vector will be able to hold exactly `capacity` elements without 624 + /// reallocating. If `capacity` is 0, the vector will not allocate. 625 + /// 626 + /// It is important to note that although the returned vector has the 627 + /// *capacity* specified, the vector will have a zero *length*. For an 628 + /// explanation of the difference between length and capacity, see 629 + /// *[Capacity and reallocation]*. 630 + /// 631 + /// [Capacity and reallocation]: #capacity-and-reallocation 632 + /// 633 + /// # Examples 634 + /// 635 + /// ``` 636 + /// #![feature(allocator_api)] 637 + /// 638 + /// use std::alloc::System; 639 + /// 640 + /// let mut vec = Vec::try_with_capacity_in(10, System).unwrap(); 641 + /// 642 + /// // The vector contains no items, even though it has capacity for more 643 + /// assert_eq!(vec.len(), 0); 644 + /// assert_eq!(vec.capacity(), 10); 645 + /// 646 + /// // These are all done without reallocating... 647 + /// for i in 0..10 { 648 + /// vec.push(i); 649 + /// } 650 + /// assert_eq!(vec.len(), 10); 651 + /// assert_eq!(vec.capacity(), 10); 652 + /// 653 + /// // ...but this may make the vector reallocate 654 + /// vec.push(11); 655 + /// assert_eq!(vec.len(), 11); 656 + /// assert!(vec.capacity() >= 11); 657 + /// 658 + /// let mut result = Vec::try_with_capacity_in(usize::MAX, System); 659 + /// assert!(result.is_err()); 660 + /// ``` 661 + #[inline] 662 + #[stable(feature = "kernel", since = "1.0.0")] 663 + pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> { 664 + Ok(Vec { buf: RawVec::try_with_capacity_in(capacity, alloc)?, len: 0 }) 660 665 } 661 666 662 667 /// Creates a `Vec<T, A>` directly from the raw components of another vector.