Apply suggested style fixes

Co-authored-by: Yuki Okushi <jtitor@2k36.org>
pull/223/head
Brent Kerby 4 years ago committed by GitHub
parent 83e41531e1
commit 0e33cf622f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,6 @@
# Allocating Memory
Using NonNull throws a wrench in an important feature of Vec (and indeed all of
Using `NonNull` throws a wrench in an important feature of Vec (and indeed all of
the std collections): creating an empty Vec doesn't actually allocate at all. This
is not the same as allocating a zero-sized memory block, which is not allowed by
the global allocator (it results in undefined behavior!). So if we can't allocate,
@ -10,7 +10,7 @@ just put some other garbage in there!
This is perfectly fine because we already have `cap == 0` as our sentinel for no
allocation. We don't even need to handle it specially in almost any code because
we usually need to check if `cap > len` or `len > 0` anyway. The recommended
Rust value to put here is `mem::align_of::<T>()`. NonNull provides a convenience
Rust value to put here is `mem::align_of::<T>()`. `NonNull` provides a convenience
for this: `NonNull::dangling()`. There are quite a few places where we'll
want to use `dangling` because there's no real allocation to talk about but
`null` would make the compiler do bad things.
@ -27,7 +27,7 @@ impl<T> Vec<T> {
ptr: NonNull::dangling(),
len: 0,
cap: 0,
_marker: PhantomData
_marker: PhantomData,
}
}
}
@ -173,7 +173,7 @@ impl<T> Vec<T> {
// This can't overflow since self.cap <= isize::MAX.
let new_cap = 2 * self.cap;
// Layout::array checks that the number of bytes is <= usize::MAX,
// `Layout::array` checks that the number of bytes is <= usize::MAX,
// but this is redundant since old_layout.size() <= isize::MAX,
// so the `unwrap` should never fail.
let new_layout = Layout::array::<T>(new_cap).unwrap();

@ -21,7 +21,7 @@ impl<T> RawVec<T> {
// !0 is usize::MAX. This branch should be stripped at compile time.
let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
// NonNull::dangling() doubles as "unallocated" and "zero-sized allocation"
// `NonNull::dangling()` doubles as "unallocated" and "zero-sized allocation"
RawVec {
ptr: NonNull::dangling(),
cap: cap,
@ -40,7 +40,7 @@ impl<T> RawVec<T> {
// This can't overflow because we ensure self.cap <= isize::MAX.
let new_cap = 2 * self.cap;
// Layout::array checks that the number of bytes is <= usize::MAX,
// `Layout::array` checks that the number of bytes is <= usize::MAX,
// but this is redundant since old_layout.size() <= isize::MAX,
// so the `unwrap` should never fail.
let new_layout = Layout::array::<T>(new_cap).unwrap();

@ -77,7 +77,6 @@ impl<T> Drop for RawVec<T> {
And change Vec as follows:
```rust,ignore
pub struct Vec<T> {
buf: RawVec<T>,
len: usize,

@ -38,7 +38,7 @@ impl<T> RawVec<T> {
// !0 is usize::MAX. This branch should be stripped at compile time.
let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
// NonNull::dangling() doubles as "unallocated" and "zero-sized allocation"
// `NonNull::dangling()` doubles as "unallocated" and "zero-sized allocation"
RawVec {
ptr: NonNull::dangling(),
cap: cap,
@ -57,7 +57,7 @@ impl<T> RawVec<T> {
// This can't overflow because we ensure self.cap <= isize::MAX.
let new_cap = 2 * self.cap;
// Layout::array checks that the number of bytes is <= usize::MAX,
// `Layout::array` checks that the number of bytes is <= usize::MAX,
// but this is redundant since old_layout.size() <= isize::MAX,
// so the `unwrap` should never fail.
let new_layout = Layout::array::<T>(new_cap).unwrap();

Loading…
Cancel
Save