-
Notifications
You must be signed in to change notification settings - Fork 182
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
Vec
improvements
#427
Closed
Closed
Vec
improvements
#427
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -821,6 +821,36 @@ impl<T, const N: usize> Vec<T, N> { | |
// All item are processed. This can be optimized to `set_len` by LLVM. | ||
drop(g); | ||
} | ||
|
||
/// Returns the remaining spare capacity of the vector as a slice of `MaybeUninit<T>`. | ||
/// | ||
/// The returned slice can be used to fill the vector with data before marking the data as | ||
/// initialized using the `set_len` method. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use heapless::Vec; | ||
/// | ||
/// // Allocate vector big enough for 10 elements. | ||
/// let mut v: Vec<_, 10> = Vec::new(); | ||
/// | ||
/// // Fill in the first 3 elements. | ||
/// let uninit = v.spare_capacity_mut(); | ||
/// uninit[0].write(0); | ||
/// uninit[1].write(1); | ||
/// uninit[2].write(2); | ||
/// | ||
/// // Mark the first 3 elements of the vector as being initialized. | ||
/// unsafe { | ||
/// v.set_len(3); | ||
/// } | ||
/// | ||
/// assert_eq!(&v, &[0, 1, 2]); | ||
/// ``` | ||
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] { | ||
&mut self.buffer[self.len..] | ||
} | ||
} | ||
|
||
// Trait implementations | ||
|
@@ -858,6 +888,17 @@ impl<T, const N: usize> Drop for Vec<T, N> { | |
} | ||
} | ||
|
||
impl<T, const N: usize> From<[T; N]> for Vec<T, N> { | ||
/// Converts array to `Vec` of same size and capacity without copying | ||
fn from(buffer: [T; N]) -> Self { | ||
Self { | ||
// cast [T; N] into [MaybeUninit<T>; N] | ||
buffer: unsafe { buffer.as_ptr().cast::<[MaybeUninit<T>; N]>().read() }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this does a bitwise copy of the elements into the Vec, but will still drop them when You can wrap the |
||
len: N, | ||
} | ||
} | ||
} | ||
|
||
impl<'a, T: Clone, const N: usize> TryFrom<&'a [T]> for Vec<T, N> { | ||
type Error = (); | ||
|
||
|
@@ -1581,4 +1622,24 @@ mod tests { | |
// Validate full | ||
assert!(v.is_full()); | ||
} | ||
|
||
#[test] | ||
fn spare_capacity_mut() { | ||
let mut v: Vec<_, 4> = Vec::new(); | ||
let uninit = v.spare_capacity_mut(); | ||
assert_eq!(uninit.len(), 4); | ||
uninit[0].write(1); | ||
uninit[1].write(2); | ||
uninit[2].write(3); | ||
unsafe { v.set_len(3) }; | ||
assert_eq!(v.as_slice(), &[1, 2, 3]); | ||
|
||
let uninit = v.spare_capacity_mut(); | ||
assert_eq!(uninit.len(), 1); | ||
uninit[0].write(4); | ||
unsafe { v.set_len(4) }; | ||
assert_eq!(v.as_slice(), &[1, 2, 3, 4]); | ||
|
||
assert!(v.spare_capacity_mut().is_empty()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
it feels a bit strange that the array length becomes the capacity. Intuitively I'd say it should be possible to convert a
[u8; 10]
into aVec<u8; 20>
with 10 filled elements.This would fail if
CAP < N
, and there's no way to addwhere CAP >= N
in const generics, so tihs'd have to be TryFrom which perhaps is not that ergonomic?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.
There is a way to generate a compile error in that case. This PR does this: #352
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.
I thought doing it this way would allow me to "move" the array into the Vec without copying, but it turns out that copying will happen either way, so might as well allow N < CAP.