-
Notifications
You must be signed in to change notification settings - Fork 33
Add a crapton of missing trait impls and docs #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 |
@@ -326,22 +331,26 @@ extern "C" { | |||
static EMPTY_HEADER: Header; | |||
} | |||
|
|||
// TODO: overflow checks everywhere | |||
|
|||
// Utils for computing layouts of allocations | |||
|
|||
fn alloc_size<T>(cap: usize) -> usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// **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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting on this line could be nicer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Backticks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...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.