turns out that was a really easy fix

This commit is contained in:
missing 2022-05-16 10:42:55 -05:00 committed by missing
parent 9cd5a6b309
commit b42bee180e
2 changed files with 10 additions and 6 deletions

View file

@ -67,7 +67,7 @@
//!
//! Finally, a vector can be `unsize`d to another vector:
//!
//! ```should_panic
//! ```
//! # use dyn_vec::prelude::{*, vec};
//! # use std::fmt::Debug;
//! let vec: Vec<i32> = vec![1, 2, 3];
@ -438,10 +438,15 @@ impl<T: ?Sized> Vec<T> {
}
/// Converts a `Vec<T: Sized>` into a `Vec<U: ?Sized>`, given that `T` can be `CoerceUnsized` into `U`.
pub fn unsize<U: ?Sized>(self) -> Vec<U> where for<'a> &'a T: CoerceUnsized<&'a U> {
#![allow(unreachable_code)]
// FIXME: when fixed, make the 2 relevant tests not should_panic (test::unsize and the line 70 doctest)
panic!("Vec::unsize is unsound rn, sorry yall");
pub fn unsize<U: ?Sized>(mut self) -> Vec<U> where for<'a> &'a T: CoerceUnsized<&'a U> {
if size_of::<Extra<U>>() > size_of::<Extra<T>>() {
let elem_size = self.end_ptr.as_ptr() as usize - self.ptr.as_ptr() as usize;
let extra_size = self.len * size_of::<Extra<U>>();
let needed_size = elem_size + extra_size;
if needed_size > self.capacity {
unsafe { self.realloc(needed_size); }
}
}
let new_vec = Vec::<U> {
ptr: self.ptr,

View file

@ -189,7 +189,6 @@ fn with_capacity() {
assert_ne!(prev_ptr, vec.as_ptr());
}
#[should_panic = "Vec::unsize is unsound rn, sorry yall"]
#[test]
fn unsize() {
let vec = vec![1, 2, 3];