Skip to content

Commit

Permalink
Merge pull request #87 from artichoke/lopopolo/into_vec_no_more_assoc…
Browse files Browse the repository at this point in the history
…_method

Make `RawParts::into_vec` a regular method
  • Loading branch information
lopopolo authored May 31, 2023
2 parents 78d70b8 + ceef291 commit 4ec9de6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ target/
!*.yaml
!*.yml

mutants.out*/
**/vendor/*
!**/vendor/*.md
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let rebuilt = unsafe {
let ptr = ptr as *mut u32;
let raw_parts = RawParts { ptr, length, capacity };

RawParts::into_vec(raw_parts)
raw_parts.into_vec()
};
assert_eq!(rebuilt, [4294967295, 0, 1]);
```
Expand Down
18 changes: 7 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
//! let ptr = ptr as *mut u32;
//! let raw_parts = RawParts { ptr, length, capacity };
//!
//! RawParts::into_vec(raw_parts)
//! raw_parts.into_vec()
//! };
//! assert_eq!(rebuilt, [4294967295, 0, 1]);
//! ```
Expand Down Expand Up @@ -86,7 +86,7 @@ use core::mem::ManuallyDrop;
/// let ptr = ptr as *mut u32;
/// let raw_parts = RawParts { ptr, length, capacity };
///
/// RawParts::into_vec(raw_parts)
/// raw_parts.into_vec()
/// };
/// assert_eq!(rebuilt, [4294967295, 0, 1]);
/// ```
Expand Down Expand Up @@ -184,7 +184,7 @@ impl<T> RawParts<T> {
/// let ptr = ptr as *mut u32;
/// let raw_parts = RawParts { ptr, length, capacity };
///
/// RawParts::into_vec(raw_parts)
/// raw_parts.into_vec()
/// };
/// assert_eq!(rebuilt, [4294967295, 0, 1]);
/// ```
Expand All @@ -207,9 +207,6 @@ impl<T> RawParts<T> {

/// Creates a `Vec<T>` directly from the raw components of another vector.
///
/// This function is declared as an associated function and must be called
/// as `RawParts::into_vec(raw_parts)`.
///
/// # Safety
///
/// This function has the same safety invariants as [`Vec::from_raw_parts`],
Expand Down Expand Up @@ -282,18 +279,17 @@ impl<T> RawParts<T> {
///
/// // Put everything back together into a Vec
/// let raw_parts = RawParts { ptr, length, capacity };
/// let rebuilt = RawParts::into_vec(raw_parts);
/// let rebuilt = raw_parts.into_vec();
/// assert_eq!(rebuilt, [4, 5, 6]);
/// }
/// ```
#[must_use]
#[allow(clippy::needless_pass_by_value)]
pub unsafe fn into_vec(this: Self) -> Vec<T> {
pub unsafe fn into_vec(self) -> Vec<T> {
let Self {
ptr,
length,
capacity,
} = this;
} = self;

// Safety:
//
Expand Down Expand Up @@ -321,7 +317,7 @@ mod tests {
let raw_parts = RawParts::from_vec(vec);
let raw_ptr = raw_parts.ptr;

let mut roundtripped_vec = unsafe { RawParts::into_vec(raw_parts) };
let mut roundtripped_vec = unsafe { raw_parts.into_vec() };

assert_eq!(roundtripped_vec.capacity(), 100);
assert_eq!(roundtripped_vec.len(), 9);
Expand Down

0 comments on commit 4ec9de6

Please sign in to comment.