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

Tracking Issue for slice_as_array #133508

Open
4 of 6 tasks
bjoernager opened this issue Nov 26, 2024 · 4 comments
Open
4 of 6 tasks

Tracking Issue for slice_as_array #133508

bjoernager opened this issue Nov 26, 2024 · 4 comments
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@bjoernager
Copy link
Contributor

bjoernager commented Nov 26, 2024

Feature gate: #![feature(slice_as_array)]

This is a tracking issue for adding conversion functions from slices to arrays.

Public API

impl<T> [T] {
    pub const fn as_array<const N: usize>(&self) -> Option<&[T; N]>;

    pub const fn as_mut_array<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_mut_array<const N: usize>(self) -> Option<*mut [T; N]>;
}

// alloc::boxed

impl<T> Box<[T]> {
    pub fn into_array<const N: usize>(self) -> Option<Box<[T; N]>>;
}

// alloc::rc

impl<T> Rc<[T]> {
    pub fn into_array<const N: usize>(self) -> Option<Rc<[T; N]>>;
}

// alloc::sync

impl<T> Arc<[T]> {
    pub fn into_array<const N: usize>(self) -> Option<Arc<[T; N]>>;
}

Steps / History

Unresolved Questions

  • const-compatible for the non-primitive types?
  • Option or Result for the owning conversions?
  • Implementation for Mutex and RwLock?
@bjoernager bjoernager added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Nov 26, 2024
@bjoernager
Copy link
Contributor Author

@RustyYato suggests the following signature for the alloc-typed slices:

pub fn into_array<const N: usize>(self) -> Result<Self<[T; N]>, Self<[T]>>;

to prevent accidental leaks if conversion fails. Would this be preferred as-is or would a new error type (e.g. SliceIntoArrayError) have to be defined?

@programmerjake
Copy link
Member

well, if you use Rc::<dyn Any>::downcast as precedence, just returning Result<Rc<[T; N]>, Rc<[T]>> (and likewise for other owning types) should be good imo

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Nov 28, 2024
Add `as_array` and `as_mut_array` conversion methods to slices.

Tracking issue: rust-lang#133508

This PR unstably implements the `as_array` and `as_mut_array` converters to `[T]`, `*const [T]`, and `*mut [T]`.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 28, 2024
Rollup merge of rust-lang#133512 - bjoernager:slice-as-array, r=Amanieu

Add `as_array` and `as_mut_array` conversion methods to slices.

Tracking issue: rust-lang#133508

This PR unstably implements the `as_array` and `as_mut_array` converters to `[T]`, `*const [T]`, and `*mut [T]`.
@bjoernager

This comment has been minimized.

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Dec 2, 2024
Fix docs for `<[T]>::as_array`.

Tracking issue: rust-lang#133508

This PR fixes a small typographical error in the docs entry for `<[T]>::as_array`.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 2, 2024
Rollup merge of rust-lang#133743 - bjoernager:slice-as-array, r=joboet

Fix docs for `<[T]>::as_array`.

Tracking issue: rust-lang#133508

This PR fixes a small typographical error in the docs entry for `<[T]>::as_array`.
@bjoernager
Copy link
Contributor Author

I am aware that they mostly do not specialise any methods, but could (and should) we extend this to also include Mutex and RwLock?

jhpratt added a commit to jhpratt/rust that referenced this issue Dec 27, 2024
Add `into_array` conversion destructors for `Box`, `Rc`, and `Arc`.

Tracking issue: rust-lang#133508

This PR adds the `into_array` destructor for `alloc::boxed::Box<[T]>`, `alloc::rc::Rc<[T]>`, and `alloc::sync::Arc<[T]>`.

Note that this PR assumes the initial proposal of these functions returning `Option`. It is still an open question whether this should instead be `Result`. We can, however, easily change this in a follow-up PR with the necessary consensus.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 27, 2024
Rollup merge of rust-lang#134379 - bjoernager:slice-as-array, r=dtolnay

Add `into_array` conversion destructors for `Box`, `Rc`, and `Arc`.

Tracking issue: rust-lang#133508

This PR adds the `into_array` destructor for `alloc::boxed::Box<[T]>`, `alloc::rc::Rc<[T]>`, and `alloc::sync::Arc<[T]>`.

Note that this PR assumes the initial proposal of these functions returning `Option`. It is still an open question whether this should instead be `Result`. We can, however, easily change this in a follow-up PR with the necessary consensus.
poliorcetics pushed a commit to poliorcetics/rust that referenced this issue Dec 28, 2024
Add `into_array` conversion destructors for `Box`, `Rc`, and `Arc`.

Tracking issue: rust-lang#133508

This PR adds the `into_array` destructor for `alloc::boxed::Box<[T]>`, `alloc::rc::Rc<[T]>`, and `alloc::sync::Arc<[T]>`.

Note that this PR assumes the initial proposal of these functions returning `Option`. It is still an open question whether this should instead be `Result`. We can, however, easily change this in a follow-up PR with the necessary consensus.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants