add more tests

This commit is contained in:
2026-07-26 14:49:00 +02:00
parent 0a8bad4d21
commit 03e4509aec
2 changed files with 20 additions and 1 deletions
+2 -1
View File
@@ -132,7 +132,8 @@ impl TypeErasedVec {
let layout = self.layout;
let res = self.try_as_type();
if cfg!(debug_assertions) && res.is_none() {
#[cfg(debug_assertions)]
if res.is_none() {
unreachable!(
"Calling `as_type_unchecked` with an incompatible layout is UB! Target type layout must exactly match the erased layout. Capacity is reserved for {:?} but {} has {:?}",
layout,
+18
View File
@@ -368,3 +368,21 @@ fn test_multiple_compatible_type_casts() {
let restored = unsafe { erased.cast_into_vec::<i32>() };
assert_eq!(restored, vec![-1, 42]);
}
#[test]
fn test_cast_and_clear() {
let mut vec = Vec::<i32>::new();
vec.push(-1);
let mut erased = TypeErasedVec::new(vec);
{
let mut guard = unsafe { erased.cast_type::<u32>() };
assert_eq!(guard.as_slice(), &[u32::MAX]);
guard.clear();
guard.push(u32::MAX - 1);
guard.push(42);
}
let restored = unsafe { erased.cast_into_vec::<i32>() };
assert_eq!(restored, vec![-2, 42]);
}