Skip to content

Tracking Issue for slice_flatten #95629

Closed

Description

@Cyborus04

These methods stabilized in #125561 for rust 1.80 🎉

This issue is now tracking their const-stability.


Feature gate: #![feature(slice_flatten)]

This is a tracking issue for the methods flatten and flatten_mut on [[T; N]], and into_flattened on Vec<[T; N], A>.

Public API

// core::slice

impl<T, const N: usize> [[T; N]] {
    pub fn as_flattened(&self) -> &[T];
    pub fn as_flattened_mut(&mut self) -> &mut [T];
}

// alloc::vec

impl<T, A: Allocator, const N: usize> Vec<[T; N], A> {
    pub fn into_flattened(self) -> Vec<T, A>;
}

Steps / History

Unresolved Questions

  • Are these the best possible names?

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-libs-api[DEPRECATED; DO NOT USE]
    on Apr 4, 2022
  2. CryZe commented on Apr 4, 2022

    @CryZe
    Contributor

    This might pose a similar problem to array/slice into_iter in that array might get a flatten as well, so we may need to be careful when stabilizing this as slice::flatten.

  3. Cyborus04 commented on Apr 4, 2022

    @Cyborus04
    ContributorAuthor

    Adding &[[T; M]; N] -> &[T; N * M] would change the type [[1, 2, 3], [4, 5, 6]].flatten() returns, but would that actually be a problem? Anywhere it was expected to be a slice, it could simply deref/coerce into one.

  4. wwylele commented on Apr 4, 2022

    @wwylele
    Contributor

    I don't think we should rely on coercion for this. In more complicated code that relies on type inference, it might not be able to correctly coerce from array to slice.

    I propose we rename this to flatten_slice, and have flatten_array for array

  5. Cyborus04 commented on Apr 4, 2022

    @Cyborus04
    ContributorAuthor

    Hmm, true.

    Another point: the case [[(); usize::MAX] usize::MAX] would likely turn into a compile-time error rather than a panic, which would technically be a breaking change, if a rather niche one.

    I like those names, the added explicitness is nice. For the mutable variants, flatten_slice_mut &flatten_array_mut, or flatten_mut_slice & flatten_mut_array? I prefer the flatten_*_mut ones.

  6. scottmcm commented on Apr 8, 2022

    @scottmcm
    Member

    We could also use into_flattened for the array version, like we currently have for the Vec version. That way it'd be distinct from the slice versions. Looking at https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv , maybe as_flattened(), as_flattened_mut(), and .into_flattened().

    That'd also address feedback like #61680 (comment) that calling this flatten is confusing since it's not exactly the same as Iterator::flatten (though I'm not convinced by that concern, personally). And it'd fit with .as_chunks(), which is the inverse method on slices.

  7. Cyborus04 commented on Apr 8, 2022

    @Cyborus04
    ContributorAuthor

    We could also use into_flattened for the array version, like we currently have for the Vec version.

    That would work for [[T; N]; M] -> [T; N * M] (indeed, I would like to include that), would it for &[[T; N]; M] -> &[T; N * M]?

    maybe as_flattened(), as_flattened_mut(), and .into_flattened().

    I like that idea, seems more consistent that way

  8. leonardo-m commented on Apr 15, 2022

    @leonardo-m

    A sufficiently common use case I've found for flatten_mut it to reset 2D matrices:

    let mut m = [[1, 2, 3], [4, 5, 6]];
    m.flatten_mut().fill(0);
    
  9. Elarcis commented on Nov 17, 2022

    @Elarcis
    Contributor

    In the case of generating a flattened slice from an array/vector, I will add a case for .flat_slice() and .flat_slice_mut().

    • The name starts with the keyword one would expect while trying to flatten their type
    • It’s short enough to write without losing meaning
    • It mentions that it creates a slice so no ambiguity
    • It doesn’t look too much like .flatten() which is something I’d expect on iterators, and which works differently

    Edit: or .as_flat_slice() and .as_flat_mut_slice() for coherence with .as_slice() and .as_mut_slice().

  10. Cyborus04 commented on Nov 26, 2022

    @Cyborus04
    ContributorAuthor

    That would make the names for equivalent array methods easier too: .{as_}flat_array(), .{as_}flat_array_ref(), and .{as_}flat_array_mut()

  11. mina86 commented on Dec 17, 2022

    @mina86
    Contributor

    Just to stir things up a little, ;) was an alternative/additional From<&[[T; N]]> for &[T] considered? I think it would solve array problem in the sense that once From<&[[T; N]; M]> for &[T; N*M] and From<[[T; N]; M]> for [T; N*M] become possible they could be added as well without having to invent new function name or affecting existing code. Though admittedly usage might be more convoluted in places where type inference cannot help.

  12. scottmcm commented on Dec 17, 2022

    @scottmcm
    Member

    @mina86 Personally, the thing I like most about .flatten() is that it's not so generic. When I call .flatten() I don't need to turbo-fish it, and I know exactly what I'm going to get.

    With .into() I might just get the &[[T; N]] back out again unless I'm extra careful. And I also really don't want to type <&[T]>::from(x) most of the time.

    There's also the potential issue that it can panic when T is a ZST. It's kinda contrived, but generally Froms try not to do that. (The obvious exception is allocation failure, but that feels pretty different from this O(1) thing.)

  13. nvzqz commented on Aug 9, 2023

    @nvzqz
    Contributor

    I created #114676 but just realized this feature also exists. Perhaps it would make sense to rename this to flatten_ref or flatten_slice?

  14. Cyborus04 commented on Aug 9, 2023

    @Cyborus04
    ContributorAuthor

    Perhaps it would make sense to rename this to flatten_ref or flatten_slice?

    I'm not sure I see why? If to not conflict with [[T; N]; M] -> [T; N * M], that feature could be named array_flatten.

  15. 66 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions