add more tests
This commit is contained in:
+2
-1
@@ -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,
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user