Skip to content

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

Merged
merged 3 commits into from
Dec 5, 2022
Merged

Add a crapton of missing trait impls and docs #40

merged 3 commits into from
Dec 5, 2022

Conversation

Gankra
Copy link
Owner

@Gankra Gankra commented Dec 5, 2022

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.

@Gankra
Copy link
Owner Author

Gankra commented Dec 5, 2022

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.
@Gankra
Copy link
Owner Author

Gankra commented Dec 5, 2022

bonus: actually handling isize::MAX stuff

@Gankra Gankra mentioned this pull request Dec 5, 2022
@@ -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 {
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// **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]];
Copy link
Contributor

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.

Copy link
Owner Author

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!
Copy link
Contributor

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backticks?

Copy link
Contributor

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.
Copy link
Contributor

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.
Copy link
Contributor

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
Copy link
Contributor

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.
Copy link
Contributor

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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.

Copy link
Contributor

@nnethercote nnethercote left a 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.

@nnethercote
Copy link
Contributor

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.

@Gankra Gankra merged commit 9e18364 into main Dec 5, 2022
Comment on lines +346 to +349
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;
Copy link

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?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...yikes! good catch.

#45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants