From 9cd5a6b30962a69a7807fc73409d44b4edb7e5cb Mon Sep 17 00:00:00 2001 From: missing Date: Mon, 16 May 2022 10:02:03 -0500 Subject: [PATCH] some docs, discovered Vec::unsize is unsound --- src/lib.rs | 23 ++++++++++++++++++++--- src/test.rs | 9 +++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1075f15..3dee056 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,9 +31,11 @@ //! Box::new(true) as _ //! ]; //! let vec_unsized: Vec = vec![unsized: 1, "foo", true]; -//! # assert_eq!(format!("{:?}", vec), "[1, 2, 3]"); +//! let vec_from_elem: Vec = vec![3; 5]; +//! # assert_eq!(vec, [1, 2, 3]); //! # assert_eq!(format!("{:?}", vec_boxed), r#"[1, "foo", true]"#); //! # assert_eq!(format!("{:?}", vec_unsized), r#"[1, "foo", true]"#); +//! # assert_eq!(vec_from_elem, [3; 5]); //! ``` //! //! A vector can be pushed to with [`Vec::push`]: @@ -63,6 +65,18 @@ //! # assert_eq!(format!("{:?}", vec), r#"[1, "foo", true, 2, "bar", false]"#); //! ``` //! +//! Finally, a vector can be `unsize`d to another vector: +//! +//! ```should_panic +//! # use dyn_vec::prelude::{*, vec}; +//! # use std::fmt::Debug; +//! let vec: Vec = vec![1, 2, 3]; +//! // vec.push_unsize("foo"); // not yet... +//! let mut vec: Vec = vec.unsize(); +//! vec.push_unsize("foo"); // now we can! +//! # assert_eq!(format!("{:?}", vec), r#"[1, 2, 3, "foo"]"#); +//! ``` +//! //! # Data Layout //! //! ```text @@ -425,6 +439,10 @@ impl Vec { /// Converts a `Vec` into a `Vec`, given that `T` can be `CoerceUnsized` into `U`. pub fn unsize(self) -> Vec 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"); + let new_vec = Vec:: { ptr: self.ptr, len: self.len, @@ -543,7 +561,6 @@ impl Drop for Vec { fn drop(&mut self) { unsafe { for i in 0..self.len { - println!("dropping {}", i); drop_in_place(self.get_unchecked_mut(i)); } self.dealloc(); @@ -581,7 +598,7 @@ impl IndexMut for Vec { /// ``` /// # use dyn_vec::prelude::{vec, Vec}; /// # use std::fmt::Debug; -/// let vec1: Vec = vec![1, 2, 3].unsize(); +/// let vec1: Vec = vec![1, 2, 3]; /// let vec2: Vec = vec![box: /// Box::new(1) as _, /// Box::new(String::from("foo")) as _, diff --git a/src/test.rs b/src/test.rs index ece4a5d..0f5c4e0 100644 --- a/src/test.rs +++ b/src/test.rs @@ -187,4 +187,13 @@ fn with_capacity() { // should have realloc'ed by now 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]; + let mut vec: Vec = vec.unsize(); + vec.push_unsize(String::from("foo")); + assert_eq!(vec.debug(), "[1, 2, 3, \"foo\"]"); } \ No newline at end of file