Skip to content

Commit f94e645

Browse files
committed
impl Error for TryReserveError
1 parent e0a8eb4 commit f94e645

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,21 @@ pub enum TryReserveError {
174174
layout: stdalloc::alloc::Layout,
175175
},
176176
}
177+
178+
// matches stdalloc::collections::TryReserveError
179+
impl core::fmt::Display for TryReserveError {
180+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
181+
f.write_str("memory allocation failed")?;
182+
let reason = match self {
183+
TryReserveError::CapacityOverflow => {
184+
" because the computed capacity exceeded the collection's maximum"
185+
}
186+
TryReserveError::AllocError { .. } => {
187+
" because the memory allocator returned an error"
188+
}
189+
};
190+
f.write_str(reason)
191+
}
192+
}
193+
194+
impl core::error::Error for TryReserveError {}

0 commit comments

Comments
 (0)