-
Notifications
You must be signed in to change notification settings - Fork 146
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
Add from_const_with_len #301
Comments
I'm open to this idea. I would love it if the function could take a slice instead of an array + length, but I don't know whether that will be possible.
We could put the new function behind its own feature gate that requires Rust 1.57. Also note that, as soon as I have time, I will be switching the main development branch to #284 which will be the basis for smallvec 2.0, which gives us a chance to increase the minimum Rust version and make other breaking changes. |
I'm not sure how you could specify the logical capacity passing just a slice, though. Maybe I'm missing something. Edit: Hm I guess you would do something like: impl<T, const N: usize> SmallVec<[T; N]> {
// ...
pub const fn from_const_slice(items: &[T]) -> Self {
if items.len() >= N {
panic!("Expected length of slice to be less than or equal to array size");
}
SmallVec {
// ... something here
}
}
// ...
} I guess I'm not sure how we can |
What are the projects thoughts on adding
from_const_and_len
version offrom_const
to be able to create aSmallVec
in a const context that does not logically occupy the full inline stack space? Like:Example Application
The reason this came up is that I'm working on a MIDI library. MIDI uses a variable length quantity (VLQ) type for integers. I'm using a
SmallVec<[u8; 4]>
as the backing storage so that a VLQ can store 4 bytes inline before spilling onto the heap. I wanted to be able to create aVLQ::ZERO
constant to represent0
stored as a VLQ, which means the backingSmallVec
needs to be logically[0]
(capacity = 1). But my only option (as far as I am aware) wasSmallVec::from_const([0; 4])
which would logically be[0, 0, 0, 0]
, which is not a valid representation of0
as a VLQ.Issues
Unfortunately,
panic!
in const contexts was only stabilized in Rust 1.57, which as far as I am aware would cause an issue since currently theconst_new
feature only requires Rust 1.51. I'm not sure how (if at all) this can be mitigated to keepfrom_const_and_len
safe while not changing the version requirements for theconst_new
feature.The text was updated successfully, but these errors were encountered: