-
Notifications
You must be signed in to change notification settings - Fork 105
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
feat(core): Add From<[T; N]> for LimitedVec #3647
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -37,6 +37,13 @@ use scale_info::{ | |||||||||||||||||||||||||||||||||||||
#[derive(Clone, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Decode, Encode, TypeInfo)] | ||||||||||||||||||||||||||||||||||||||
pub struct LimitedVec<T, E, const N: usize>(Vec<T>, PhantomData<E>); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
impl<T, E, const M: usize, const N: usize> From<[T; M]> for LimitedVec<T, E, N> { | ||||||||||||||||||||||||||||||||||||||
fn from(value: [T; M]) -> Self { | ||||||||||||||||||||||||||||||||||||||
assert!(M <= N); | ||||||||||||||||||||||||||||||||||||||
Self(value.into(), Default::default()) | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Comment on lines
+40
to
+46
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.
Suggested change
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. please also replace places where 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 is still runtime assertion isnt it? 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.
No |
||||||||||||||||||||||||||||||||||||||
/// Formatter for [`LimitedVec`] will print to precision of 8 by default, to print the whole data, use `{:+}`. | ||||||||||||||||||||||||||||||||||||||
impl<T: Clone + Default, E: Default, const N: usize> Display for LimitedVec<T, E, N> | ||||||||||||||||||||||||||||||||||||||
where | ||||||||||||||||||||||||||||||||||||||
|
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'm against of assertions in runtime for such a type, that's mainly used in non-test code. It's not worth adding potentially not checked to be unreachable by tests panic in the trait impl that's needed only for tests.
If there's no other stable solution, then it seems better to close the PR and wait for stable.
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.
+1
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 no way to use static assertions without unstable features. static_assert!() doesnt work with const generics and everything else is gated behind unstable features. Unless it is fine to use them, which you have specified it isn't, then I'm going to close this PR until some feature becomes stable.