Like `with_capacity` but returns an error on allocation failure.
(capacity: usize)
| 70 | |
| 71 | /// Like `with_capacity` but returns an error on allocation failure. |
| 72 | pub fn try_with_capacity(capacity: usize) -> Result<Self, OutOfMemory> |
| 73 | where |
| 74 | V: Default, |
| 75 | { |
| 76 | let mut elems = Vec::new(); |
| 77 | elems |
| 78 | .try_reserve(capacity) |
| 79 | .map_err(|_| OutOfMemory::new(core::mem::size_of::<V>().saturating_mul(capacity)))?; |
| 80 | Ok(Self { |
| 81 | elems, |
| 82 | default: Default::default(), |
| 83 | unused: PhantomData, |
| 84 | }) |
| 85 | } |
| 86 | |
| 87 | /// Create a new empty map with a specified default value. |
| 88 | /// |
nothing calls this directly
no test coverage detected