-
Notifications
You must be signed in to change notification settings - Fork 20
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
ACP: Add try_from_slice
constructor to core::array
.
#496
Comments
If we are adding a dedicated conversion function I think flipping the order turning it into an inherent method of the slice is more convenient to the users impl<T> [T] {
pub const fn as_array_exact<const N: usize>(&self) -> Result<&[T; N], TryFromSliceError>;
} I'm not sure why the ACP proposed |
I see your point, although I feel it would be confusing to have the error type named As for your second concern, I'm not quite sure I understand what the problem is. The semantics proposed in this ACP are basically the same as the preexisting; the logic has just been moved from |
I think |
For what it's worth, in the meantime this works in a const context, though only on nightly for now: if let ([a], []) = s.as_chunks() {
Some(a)
} else {
None
} |
impl<T> [T] {
pub const fn as_array<const N: usize>(&self) -> Option<&[T; N]>;
pub const fn as_array_mut<const N: usize>(&mut self) -> Option<&mut [T; N]>;
} seems pretty reasonable to me. Would be convenient to have it in const, and the (I don't think it needs to return a Result, because the error can't say anything meaningful -- just like how |
I'm personally content with that solution. |
There are two pre-existing |
In any case, would this ACP have to be changed to instead propose the |
We discussed this in the libs-api meeting and we are in favor of adding methods on slices, but also on raw pointers to slices and Box/Rc/Arc of slices: impl<T> [T] {
pub const fn as_array<const N: usize>(&self) -> Option<&[T; N]>;
pub const fn as_array_mut<const N: usize>(&mut self) -> Option<&mut [T; N]>;
}
impl<T> *const [T] {
pub const fn as_array<const N: usize>(self) -> Option<*const [T; N]>;
}
impl<T> *mut [T] {
pub const fn as_array_mut<const N: usize>(self) -> Option<*mut [T; N]>;
}
impl<T> Box<[T]> {
pub const fn into_array<const N: usize>(self) -> Option<Box<[T; N]>>;
}
impl<T> Rc<[T]> {
pub const fn into_array<const N: usize>(self) -> Option<Rc<[T; N]>>;
}
impl<T> Arc<[T]> {
pub const fn into_array<const N: usize>(self) -> Option<Arc<[T; N]>>;
} Feel free to open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. |
For the owning versions, this should return a result so that the pointer can be reused on failure. I.e. |
Is |
Proposal
Problem statement
In Rust, you can convert a slice
&[T]
to an array[T; N]
using<[T; N] as TryFrom<&[T]>>::try_from
. The main issue with this is that it depends on a trait function, and trait functions are not allowed in constant expressions (e.g.const fn
).I propose adding a
try_from_slice
function to thecore::array
module for this scenario. The module in question already defines other constructors such asfrom_fn
andfrom_ref
.Solution sketch
The following function should be added to the standard library:
Alternatives
The main alternative to adding this feature (with regard to
const
) would be to allow trait functions in constant expressions. This is already covered byconst_trait_impl
.I still do believe adding this function can improve clarity in some code. Other areas of the standard library already define similar patterns, e.g.
String
implementsInto<Box<str>>
whilst also defining theinto_boxed_str
destructor.Links and related work
Initial, unstable implementation: #133439
The text was updated successfully, but these errors were encountered: