Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

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.

fn from(value: [T; M]) -> Self {
assert!(M <= N);
Self(value.into(), Default::default())
}
}

Comment on lines +40 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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())
}
}
struct LessThenOrEqual<const M: usize, const N: usize> {}
impl<const M: usize, const N: usize> LessThenOrEqual<M, N> {
const ASSERTION: () = assert!(M <= N);
}
impl<T, E, const M: usize, const N: usize> From<[T; M]> for LimitedVec<T, E, N> {
fn from(value: [T; M]) -> Self {
let _ = LessThenOrEqual::<M, N>::ASSERTION;
Self(value.into(), Default::default())
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also replace places where ::from can be used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still runtime assertion isnt it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is still runtime assertion isnt it?

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
Expand Down