Skip to content

Tracking issue for slice::{ref_slice, mut_ref_slice} #27774

Closed

Description

@alexcrichton

This is a tracking issue for the unstable ref_slice feature in the standard library. These functions seem fairly innocuous but it's also somewhat questionable how useful they are. They're probably ready for either stabilization or deprecation as-is.

cc @nikomatsakis

Activity

  1. added
    T-libs-api[DEPRECATED; DO NOT USE]
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    on Aug 12, 2015
  2. nikomatsakis commented on Aug 13, 2015

    @nikomatsakis
    Contributor

    Speaking personally, I love these functions. I don't need 'em often, but when I do, I'm happy I have them. =)

  3. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    on Aug 13, 2015
  4. SimonSapin commented on Aug 13, 2015

    @SimonSapin
    Contributor

    Although they are unsafe, the implementations of these functions are one-liners (based on slice::from_raw_parts{,_mut}) that can be easily replicated outside of std. So I wouldn’t miss them too much.

    (Compare with functionality that may be rarely used but is impossibly to reasonably implement outside of std because e.g. it requires accessing private fields of std types.)

  5. alexcrichton commented on Sep 17, 2015

    @alexcrichton
    MemberAuthor

    Nominating for 1.5 resolution (I'd personally want to deprecate)

  6. tbu- commented on Sep 17, 2015

    @tbu-
    Contributor

    It's a safe abstraction of unsafe code, I think it would be suited for the standard library.

  7. nikomatsakis commented on Sep 17, 2015

    @nikomatsakis
    Contributor

    Just to be clear for where I use this (in case you don't know), it's in a situation like this. Often I have an enum where the variants have different numbers of things associated with them (let's say integers here). Sometimes it's handy to match and only have to deal with the exact right number of integers for each case, but other times I want to just walk over all the integers. For some reason, it often happens that the variants have either 0, 1, or N such things. This works out quite nicely:

    enum Foo {
        A, // no integers
        B(u32), // 1 integer
        C(Vec<u32>), // >1 integer
    }
    
    impl Foo {
        fn get_integers(&self) -> &[u32] {
            match *self {
                Foo::A => &[],
                Foo::B(ref i) => ref_slice(i),
                Foo::C(ref v) => v,
            }
        }
    }
    

    This seems to come up semi-regularly for me. (Sadly, the methods that let you go from &(T, T) to &[T] were removed, or it would work even for an arbitrary number of integers.)

    The other situation where this fn comes in handy for me is when you have some generic helper that takes a &[T], but you happen to have an &T lying around. You can easily reuse this helper.

    I think it's ok for this to live in libstd. It's not really duplicating anything else; I mean it's true you can write them in terms of other APIs, but only by dropping into unsafe code and going into a whole 'nother level of abstraction.

    (That said, if there were to be another crate that ALSO included the ability to convert tuples into slices and so forth, that's more interesting. Though that's probably something we can't "safely" do until we agree to commit to the way tuples are represented in memory, though I could never imagine us changing this.)

  8. alexcrichton commented on Sep 24, 2015

    @alexcrichton
    MemberAuthor

    This issue is now entering its cycle-long FCP for resolution in 1.5

    The libs team was a little up in the air about deprecation or stabilization here, comments are certainly welcome!

  9. added
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    and removed on Sep 24, 2015
  10. abonander commented on Sep 24, 2015

    @abonander
    Contributor

    👍 It's somewhat superfluous, yes, but being that it's a safe wrapper around unsafe code it's not a bad abstraction overall. It'd just end up in some utility crate somewhere, like the Apache Commons for Rust.

  11. Stebalien commented on Sep 25, 2015

    @Stebalien
    Contributor

    Please stabilize these. Personally, I use them for reading single bytes from a reader (with read_exact).

  12. briansmith commented on Sep 25, 2015

    @briansmith
    Contributor

    The documentation says "Converts a pointer to A into a slice of length 1 (without copying)." Shouldn't "pointer" be "reference"?

  13. 33 remaining items

  14. added a commit that references this issue on Oct 25, 2015
    ff49733
  15. added a commit that references this issue on Oct 25, 2015
    e02ada6
  16. added a commit that references this issue on Nov 2, 2017
  17. nox commented on Sep 19, 2019

    @nox
    Contributor

    Note that with the recent effort to reduce the amount of dependencies in some projects, we end up downstream copying the functions in their own crate rather than having one more dependency.

    This is IMO quite unfortunate and that's more unsafe code to review for everyone.

    https://github.com/CraneStation/cranelift/blob/c47ca7bafc8fc48358f1baa72360e61fc1f7a0f2/cranelift-codegen/src/ref_slice.rs#L1-L8

  18. Marwes commented on Sep 19, 2019

    @Marwes
    Contributor
  19. nox commented on Sep 19, 2019

    @nox
    Contributor

    OH WOW, I never saw that. Thanks!

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

    B-unstableBlocker: Implemented in the nightly compiler and unstable.T-libs-api[DEPRECATED; DO NOT USE]final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions