Conversation
|
r? @nnethercote |
This is almost entirely copy-pasting from std. I also fleshed out a bunch of docs in Drain. This is a yakshave to prep for adding splice support.
|
bonus: actually handling isize::MAX stuff |
|
|
||
| // Utils for computing layouts of allocations | ||
|
|
||
| fn alloc_size<T>(cap: usize) -> usize { |
There was a problem hiding this comment.
Add a doc comment to alloc_size explaining that it can panic?
src/lib.rs
Outdated
| /// If it is important to know the exact allocated capacity of a `ThinVec`, | ||
| /// always use the [`capacity`] method after construction. | ||
| /// | ||
| /// **NOTE**: unlike Vec, ThinVec **MUST** allocate to keep track of non-zero |
There was a problem hiding this comment.
Nit: Vec and ThinVec here don't have backticks. I'm not sure how hard you're trying to get every one.
src/lib.rs
Outdated
| /// If it is important to know the exact allocated capacity of a `ThinVec`, | ||
| /// always use the [`capacity`] method after construction. | ||
| /// | ||
| /// **NOTE**: unlike Vec, ThinVec **MUST** allocate to keep track of non-zero |
There was a problem hiding this comment.
| /// **NOTE**: unlike Vec, ThinVec **MUST** allocate to keep track of non-zero | |
| /// **NOTE**: unlike Vec, ThinVec **MUST** allocate once to keep track of non-zero |
| /// | ||
| /// let mut vec = thin_vec![thin_vec![1, 0, 0], | ||
| /// thin_vec![0, 1, 0], | ||
| /// thin_vec![0, 0, 1]]; |
There was a problem hiding this comment.
Formatting on this line could be nicer.
There was a problem hiding this comment.
This is the formatting std had shrug
src/lib.rs
Outdated
| /// Convert a boxed slice into a vector by transferring ownership of | ||
| /// the existing heap allocation. | ||
| /// | ||
| /// **NOTE:** unlike std, this must reallocate to change the layout! |
There was a problem hiding this comment.
std? (i.e. with backticks)
src/lib.rs
Outdated
| } | ||
|
|
||
| impl<T> From<Vec<T>> for ThinVec<T> { | ||
| /// Convert a std::Vec into a ThinVec. |
There was a problem hiding this comment.
More cases below, I won't point them all out, a bit of grepping will find them if you care to change them.
src/lib.rs
Outdated
| /// assert_eq!(ThinVec::from(b), thin_vec![1, 2, 3]); | ||
| /// ``` | ||
| fn from(s: Vec<T>) -> Self { | ||
| // Can just lean on the fact that `Box<[T]>` -> `Vec<T>` is Free. |
There was a problem hiding this comment.
Why is Box<[T]> relevant here?
src/lib.rs
Outdated
| /// assert_eq!(Vec::from(b), vec![1, 2, 3]); | ||
| /// ``` | ||
| fn from(s: ThinVec<T>) -> Self { | ||
| // Can just lean on the fact that `Box<[T]>` -> `Vec<T>` is Free. |
src/lib.rs
Outdated
| /// It's ok to use IterMut here because it promises to only take mutable | ||
| /// refs to the parts we haven't yielded yet. | ||
| /// | ||
| /// A downside of this (and the *mut below is that it makes this Invariant, when |
There was a problem hiding this comment.
This sentence reads strangely. Are the parentheses in the wrong place? Also, "Inductive" doesn't need to be capitalized?
| /// completely done with the IterMut in the `drop` impl of this type (or miri will get mad). | ||
| /// | ||
| /// Since we set the `len` of this to be before `IterMut`, we can use that `len` | ||
| /// to retrieve the index of the start of the drain range later. |
There was a problem hiding this comment.
Nice comment! I think you enjoyed writing that one :)
src/lib.rs
Outdated
| #[must_use] | ||
| pub fn as_slice(&self) -> &[T] { | ||
| // SAFETY: this is A-OK because the elements that the underlying | ||
| // iterator still points at are still logically initialized and continguous. |
There was a problem hiding this comment.
| // iterator still points at are still logically initialized and continguous. | |
| // iterator still points at are still logically initialized and contiguous. |
unless "continguous" is a new word The Kids Are Using These Days.
nnethercote
left a comment
There was a problem hiding this comment.
Looks good. A few minor suggestions. I admit I didn't read all the example code in the doc comments, though I did read the comment prose and the code in the tests.
|
Note: there's a clippy warning that might be worth using an attribute to placate. GitHub wouldn't let me comment on the specific line for some reason. |
| let cap = cap as isize; | ||
| let header_size = mem::size_of::<Header>() as isize; | ||
| let elem_size = mem::size_of::<T>() as isize; | ||
| let padding = padding::<T>() as isize; |
There was a problem hiding this comment.
Apologies for dropping in belatedly, but I was just reviewing changes between v0.2.9 and v0.2.11 (for rust-lang/rust#108245) and spotted this...
Isn't there a risk that these casts (okay, less so the latter three but certainly the cap one) might silently overflow isize::MAX and result in a negative isize value? Better to use TryInto, I think?
There was a problem hiding this comment.
...yikes! good catch.
This is almost entirely copy-pasting from std. I also fleshed out a bunch of docs in Drain. This is a yakshave to prep for adding splice support.