Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upTracking issue for the `SliceIndex` trait #35729
Comments
alexcrichton
added
T-libs
B-unstable
B-RFC-approved
and removed
B-unstable
labels
Aug 16, 2016
alexcrichton
referenced this issue
Aug 16, 2016
Merged
Add panic-safe indexing methods round 2 #1679
sfackler
self-assigned this
Aug 16, 2016
briansmith
referenced this issue
Aug 17, 2016
Closed
Implemengt NoPanicSlice in terms of non-panicing slicing in when it is available in Rust's standard library #138
utkarshkukreti
referenced this issue
Nov 13, 2016
Closed
Speed up `std::fmt::Write::write_str` for `maud::Escaper`. #61
solidsnack
referenced this issue
Nov 14, 2016
Closed
`Index` trait should provide `get(&self, key: &Q) -> Option<&V>` #1790
bors
added a commit
that referenced
this issue
Nov 27, 2016
sfackler
added
B-RFC-implemented
and removed
B-RFC-approved
labels
Nov 27, 2016
This comment has been minimized.
This comment has been minimized.
|
This is now implemented and will land in the 1.15 release! |
sfackler
closed this
Nov 28, 2016
This comment has been minimized.
This comment has been minimized.
|
Oh right, this should be the tracking issue for the unstable helper traits... |
sfackler
reopened this
Nov 28, 2016
sfackler
added
the
B-unstable
label
Nov 28, 2016
sfackler
changed the title
Tracking issue for adding panic safe slicing methods
Tracking issue for the `SliceIndex` trait
Nov 28, 2016
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Feb 18, 2017
|
Could we please stabilize this for the next release (or the next next release)? It seems like it is possible to use the new functionality, but it isn't possible to write (transparent) wrappers around it because any reference to the |
This comment has been minimized.
This comment has been minimized.
|
@sfackler I forget, was this an intended "never to be stabilized" trait or did we want this on the track to stabilization? @briansmith note that the trait can be vendored locally for now (if necessary) as it's not unstable to implement it, just to use the libstd version. Now that's a huge PITA (especially in multiple crates), but it at least gets the ergonomics immediately! |
This comment has been minimized.
This comment has been minimized.
|
I wouldn't call it a "never stabilize" kind of thing, but it's definitely a bit gross in its current form. One suggestion on the RFC was to split out a trait per method which could be a bit cleaner and more extensible in the future. |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Feb 21, 2017
|
@alexcrichton I don't know what you mean by "can be vendored." Here's what I'm trying to write: #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Slice<'a> {
bytes: &'a [u8]
}
impl<'a> Slice<'a> {
#[inline]
pub fn new(bytes: &'a [u8]) -> Slice<'a> {
Slice { bytes: bytes }
}
#[inline]
pub fn get<I>(&self, i: I) -> Option<&I::Output>
where I: std::slice::SliceIndex<u8> {
self.bytes.get(i)
}
}The result is "use of unstable library feature 'slice_get_slice' (see issue #35729))." |
This comment has been minimized.
This comment has been minimized.
|
@briansmith oh I just mean that you can add your own That'd work for one crate to get ergonomics, but it wouldn't work at large to continue abstracting over it. |
This comment has been minimized.
This comment has been minimized.
|
It'd be nice to see this trait combined with |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton I'm not sure what you mean in your recommendation to @briansmith? I want to take a generic slice index argument (i.e., may be a range), and then use it to index into a slice. If I copy the trait into my own crate, I can't then take something implementing that trait and pass to |
This comment has been minimized.
This comment has been minimized.
|
@jonhoo oh mean basically just copy/pasting the trait in libstd into your own crate. You wouldn't call |
jonhoo
added a commit
to jonhoo/rust-ibverbs
that referenced
this issue
May 11, 2017
Mark-Simulacrum
added
the
C-tracking-issue
label
Jul 22, 2017
ndusart
added a commit
to ndusart/nix
that referenced
this issue
Jul 24, 2017
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Apr 20, 2018
|
The final comment period is now complete. |
Centril
added
disposition-merge
finished-final-comment-period
and removed
final-comment-period
labels
May 24, 2018
tmccombs
added a commit
to tmccombs/rust
that referenced
this issue
May 29, 2018
This comment has been minimized.
This comment has been minimized.
|
#51147 implements #35729 (comment) except |
kennytm
added a commit
to kennytm/rust
that referenced
this issue
May 30, 2018
kennytm
added a commit
to kennytm/rust
that referenced
this issue
May 30, 2018
Mark-Simulacrum
added a commit
to Mark-Simulacrum/rust
that referenced
this issue
Jun 2, 2018
bors
closed this
in
9d770e9
Jun 3, 2018
This comment has been minimized.
This comment has been minimized.
|
Would it be possible to add a blanket impl for |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Oh wait, I just remembered: that would replace specialized code with enum-based dispatch. I don’t know if that ends up being reliably optimized away. |
This comment has been minimized.
This comment has been minimized.
|
Couldn't we use impl specialization? |
joshlf
referenced this issue
Jun 9, 2018
Closed
Implement SliceIndex for R: RangeBounds<usize> #51464
This comment has been minimized.
This comment has been minimized.
|
If this doesn’t simplify the code, why is this blanker impl useful? |
This comment has been minimized.
This comment has been minimized.
|
It's useful because otherwise you can't write the following code (which I tried to do, which motivated my commenting here): fn foo<R: RangeBounds<usize>>(buf: &mut [u8], range: R) {
bar(&mut buf[range]);
} |
This comment has been minimized.
This comment has been minimized.
|
Can you have a bound on |
This comment has been minimized.
This comment has been minimized.
|
Unfortunately not - some of the code actually makes use of the methods on Click to expand
|
This comment has been minimized.
This comment has been minimized.
|
… then have two bounds? |
This comment has been minimized.
This comment has been minimized.
|
That doesn't work because generic code needs to call Click to expand pub fn slice_mut<'a, T, R: RangeBounds<usize>>(slc: &'a mut [T], range: &R) -> &'a mut [T] {
let lower = resolve_lower_bound(range.start_bound());
let upper = resolve_upper_bound(slc.len(), range.end_bound()).unwrap();
&mut slc[lower..upper]
}
// return the inclusive equivalent of the bound
fn resolve_lower_bound(bound: Bound<&usize>) -> usize {
match bound {
Bound::Included(x) => *x,
Bound::Excluded(x) => *x + 1,
Bound::Unbounded => 0,
}
}
// return the exclusive equivalent of the bound, verifying that it is in
// range of len
fn resolve_upper_bound(len: usize, bound: Bound<&usize>) -> Option<usize> {
let bound = match bound {
Bound::Included(x) => *x + 1,
Bound::Excluded(x) => *x,
Bound::Unbounded => len,
};
if bound > len {
return None;
}
Some(bound)
} |
This comment has been minimized.
This comment has been minimized.
|
Oh, sorry, I guess you mean have the bound |
This comment has been minimized.
This comment has been minimized.
|
Yes, if you want to do with a generic type two things that each require a different trait, have your own code require both traits. |
This comment has been minimized.
This comment has been minimized.
|
Oh, and one other issue. Since |
This comment has been minimized.
This comment has been minimized.
|
This higher-ranked trait bounds rather than types, but yeah, good point. |
alexcrichton commentedAug 16, 2016
•
edited by sfackler
Tracking issue for rust-lang/rfcs#1679
The implementations are stable, but the
SliceIndextrait is not.