Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions text/0000-slice-tail-redesign.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
- Feature Name: `slice_tail_redesign`
- Start Date: 2015-04-11
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary

Replace `slice.tail()`, `slice.init()` with new methods `slice.shift_first()`,
`slice.shift_last()`.

# Motivation

The `slice.tail()` and `slice.init()` methods are relics from an older version
of the slice APIs that included a `head()` method. `slice` no longer has
`head()`, instead it has `first()` which returns an `Option`, and `last()` also
returns an `Option`. While it's generally accepted that indexing / slicing
should panic on out-of-bounds access, `tail()`/`init()` are the only
remaining methods that panic without taking an explicit index.

A conservative change here would be to simply change `head()`/`tail()` to return
`Option`, but I believe we can do better. These operations are actually
specializations of `split_at()` and should be replaced with methods that return
`Option<(T,&[T])>`. This makes the common operation of processing the first/last
element and the remainder of the list more ergonomic, with very low impact on
code that only wants the remainder (such code only has to add `.1` to the
expression). This has an even more significant effect on code that uses the
mutable variants.

# Detailed design

The methods `head()`, `tail()`, `head_mut()`, and `tail_mut()` will be removed,
and new methods will be added:

```rust
fn shift_first(&self) -> Option<(&T, &[T])>;
fn shift_last(&self) -> Option<(&T, &[T])>;
fn shift_first_mut(&mut self) -> Option<(&mut T, &mut [T])>;
fn shift_last_mut(&mut self) -> Option<(&mut T, &mut [T])>;
```

Existing code using `tail()` or `init()` could be translated as follows:

* `slice.tail()` becomes `slice.shift_first().unwrap().1` or `&slice[1..]`
* `slice.init()` becomes `slice.shift_last().unwrap().1` or
`&slice[..slice.len()-1]`

It is expected that a lot of code using `tail()` or `init()` is already either
testing `len()` explicitly or using `first()` / `last()` and could be refactored
to use `shift_first()` / `shift_last()` in a more ergonomic fashion. As an
example, the following code from typeck:

```rust
if variant.fields.len() > 0 {
for field in variant.fields.init() {
```

can be rewritten as:

```rust
if let Some((_, init_fields)) = variant.fields.shift_last() {
for field in init_fields {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vs

let len = variant.fields.len();
if len > 0 {
    for field in &variant.fields[.. len - 1] {

vs

variant.fields.shift_last().map(|(_, init_fields)| {
   for field in init_fields {

(I find if-let to be really noisy, especially when working with options)

```

And the following code from compiletest:

```rust
let argv0 = args[0].clone();
let args_ = args.tail();
```

can be rewritten as:

```rust
let (argv0, args_) = args.shift_first().unwrap();
```

(the `clone()` ended up being unnecessary).

# Drawbacks

The expression `slice.shift_last().unwrap.1` is more cumbersome than
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap()

`slice.init()`. However, this is primarily due to the need for `.unwrap()`
rather than the need for `.1`, and would affect the more conservative solution
(of making the return type `Option<&[T]>`) as well.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a non-sequitur. The idiomatic translation is:

  • slice.tail() -> &slice[1..]
  • slice.init() -> &slice[..slice.len() - 1]

No? Basically init/tail have really just been superceeded by the vastly more flexible slicing syntax (we don't even need to have _mut variants because of slicing, woo!)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you potentially typo and write &slice[..slice.len()]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&slice[..slice.len() - 1] only works if you have the slice stored in a variable. If it's the result of some expression, you need to introduce a temporary variable to do that. Which is why I used slice.shift_last().unwrap.1 instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also to note, the &slice[..slice.len()-1] alternative was given up above on line 45, this drawback is only calling out the awkwardness of the shift_last() approach.


# Alternatives

Only change the return type to `Option` without adding the tuple. This is the
more conservative change mentioned above. It still has the same drawback of
requiring `.unwrap()` when translating existing code. And it's unclear what the
function names should be (the current names are considered suboptimal).

# Unresolved questions

Is the name correct? There's precedent in this name in the form of
[`str::slice_shift_char()`][slice_shift_char]. An alternative name might be
`pop_first()`/`pop_last()`, or `shift_front()`/`shift_back()` (although the
usage of `first`/`last` was chosen to match the existing methods `first()` and
`last()`). Another option is `split_first()`/`split_last()`.

Should `shift_last()` return `Option<(&T, &[T])>` or `Option<(&[T], &T)>`?
I believe that the former is correct with this name, but the latter might be
more suitable given the name `split_last()`.

[slice_shift_char]: http://doc.rust-lang.org/nightly/std/primitive.str.html#method.slice_shift_char