about summary refs log tree commit diff
path: root/src/liballoc/benches/slice.rs
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2017-12-16 15:29:09 -0500
committerCorey Farwell <coreyf@rwell.org>2017-12-24 23:01:24 -0800
commit66ef6b9c0995cc678a00f4d061ba8e6adb16f610 (patch)
tree2b53c1439efea0529d62b496929088d5e822114b /src/liballoc/benches/slice.rs
parentae65dcc30f421fd91b9e2d67cf77c86bfb20ee3a (diff)
downloadrust-66ef6b9c0995cc678a00f4d061ba8e6adb16f610.tar.gz
rust-66ef6b9c0995cc678a00f4d061ba8e6adb16f610.zip
Deprecate [T]::rotate in favor of [T]::rotate_{left,right}.
Background
==========

Slices currently have an unstable [`rotate`] method which rotates
elements in the slice to the _left_ N positions. [Here][tracking] is the
tracking issue for this unstable feature.

```rust
let mut a = ['a', 'b' ,'c', 'd', 'e', 'f'];
a.rotate(2);
assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']);
```

Proposal
========

Deprecate the [`rotate`] method and introduce `rotate_left` and
`rotate_right` methods.

```rust
let mut a = ['a', 'b' ,'c', 'd', 'e', 'f'];
a.rotate_left(2);
assert_eq!(a, ['c', 'd', 'e', 'f', 'a', 'b']);
```

```rust
let mut a = ['a', 'b' ,'c', 'd', 'e', 'f'];
a.rotate_right(2);
assert_eq!(a, ['e', 'f', 'a', 'b', 'c', 'd']);
```

Justification
=============

I used this method today for my first time and (probably because I’m a
naive westerner who reads LTR) was surprised when the docs mentioned that
elements get rotated in a left-ward direction. I was in a situation
where I needed to shift elements in a right-ward direction and had to
context switch from the main problem I was working on and think how much
to rotate left in order to accomplish the right-ward rotation I needed.

Ruby’s `Array.rotate` shifts left-ward, Python’s `deque.rotate` shifts
right-ward. Both of their implementations allow passing negative numbers
to shift in the opposite direction respectively.

Introducing `rotate_left` and `rotate_right` would:

- remove ambiguity about direction (alleviating need to read docs 😉)
- make it easier for people who need to rotate right

[`rotate`]: https://doc.rust-lang.org/std/primitive.slice.html#method.rotate
[tracking]: https://github.com/rust-lang/rust/issues/41891
Diffstat (limited to 'src/liballoc/benches/slice.rs')
-rw-r--r--src/liballoc/benches/slice.rs2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/liballoc/benches/slice.rs b/src/liballoc/benches/slice.rs
index 17538d885f8..ee5182a1d46 100644
--- a/src/liballoc/benches/slice.rs
+++ b/src/liballoc/benches/slice.rs
@@ -343,7 +343,7 @@ macro_rules! rotate {
         fn $name(b: &mut Bencher) {
             let size = mem::size_of_val(&$gen(1)[0]);
             let mut v = $gen($len * 8 / size);
-            b.iter(|| black_box(&mut v).rotate(($mid*8+size-1)/size));
+            b.iter(|| black_box(&mut v).rotate_left(($mid*8+size-1)/size));
             b.bytes = (v.len() * size) as u64;
         }
     }