2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2018-09-25 16:51:35 -05:00
|
|
|
#![allow(unused_variables)]
|
2018-08-30 07:18:55 -05:00
|
|
|
|
2014-09-15 03:48:58 -05:00
|
|
|
// Test slicing sugar.
|
|
|
|
|
|
|
|
extern crate core;
|
2015-01-27 22:06:46 -06:00
|
|
|
use core::ops::{Index, IndexMut, Range, RangeTo, RangeFrom, RangeFull};
|
2014-09-15 03:48:58 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
static mut COUNT: usize = 0;
|
2014-09-15 03:48:58 -05:00
|
|
|
|
|
|
|
struct Foo;
|
|
|
|
|
2015-01-03 22:43:24 -06:00
|
|
|
impl Index<Range<Foo>> for Foo {
|
|
|
|
type Output = Foo;
|
2015-03-21 20:15:47 -05:00
|
|
|
fn index(&self, index: Range<Foo>) -> &Foo {
|
2014-09-15 03:48:58 -05:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2015-01-03 22:43:24 -06:00
|
|
|
}
|
|
|
|
impl Index<RangeTo<Foo>> for Foo {
|
|
|
|
type Output = Foo;
|
2015-03-21 20:15:47 -05:00
|
|
|
fn index(&self, index: RangeTo<Foo>) -> &Foo {
|
2014-09-15 03:48:58 -05:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2015-01-03 22:43:24 -06:00
|
|
|
}
|
|
|
|
impl Index<RangeFrom<Foo>> for Foo {
|
|
|
|
type Output = Foo;
|
2015-03-21 20:15:47 -05:00
|
|
|
fn index(&self, index: RangeFrom<Foo>) -> &Foo {
|
2014-09-15 03:48:58 -05:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2015-01-03 22:43:24 -06:00
|
|
|
}
|
2015-01-27 22:06:46 -06:00
|
|
|
impl Index<RangeFull> for Foo {
|
2015-01-03 22:43:24 -06:00
|
|
|
type Output = Foo;
|
2015-03-21 20:15:47 -05:00
|
|
|
fn index(&self, _index: RangeFull) -> &Foo {
|
2014-09-15 03:48:58 -05:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 22:43:24 -06:00
|
|
|
impl IndexMut<Range<Foo>> for Foo {
|
2015-03-21 20:15:47 -05:00
|
|
|
fn index_mut(&mut self, index: Range<Foo>) -> &mut Foo {
|
2014-09-15 03:48:58 -05:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2015-01-03 22:43:24 -06:00
|
|
|
}
|
|
|
|
impl IndexMut<RangeTo<Foo>> for Foo {
|
2015-03-21 20:15:47 -05:00
|
|
|
fn index_mut(&mut self, index: RangeTo<Foo>) -> &mut Foo {
|
2014-09-15 03:48:58 -05:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2015-01-03 22:43:24 -06:00
|
|
|
}
|
|
|
|
impl IndexMut<RangeFrom<Foo>> for Foo {
|
2015-03-21 20:15:47 -05:00
|
|
|
fn index_mut(&mut self, index: RangeFrom<Foo>) -> &mut Foo {
|
2014-09-15 03:48:58 -05:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2015-01-03 22:43:24 -06:00
|
|
|
}
|
2015-01-27 22:06:46 -06:00
|
|
|
impl IndexMut<RangeFull> for Foo {
|
2015-03-21 20:15:47 -05:00
|
|
|
fn index_mut(&mut self, _index: RangeFull) -> &mut Foo {
|
2014-09-15 03:48:58 -05:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2015-01-02 18:34:13 -06:00
|
|
|
|
2015-01-03 22:43:24 -06:00
|
|
|
|
2014-09-15 03:48:58 -05:00
|
|
|
fn main() {
|
|
|
|
let mut x = Foo;
|
2021-06-18 02:09:40 -05:00
|
|
|
let _ = &x[..];
|
|
|
|
let _ = &x[Foo..];
|
|
|
|
let _ = &x[..Foo];
|
|
|
|
let _ = &x[Foo..Foo];
|
|
|
|
let _ = &mut x[..];
|
|
|
|
let _ = &mut x[Foo..];
|
|
|
|
let _ = &mut x[..Foo];
|
|
|
|
let _ = &mut x[Foo..Foo];
|
2014-09-15 03:48:58 -05:00
|
|
|
unsafe {
|
2015-06-07 13:00:38 -05:00
|
|
|
assert_eq!(COUNT, 8);
|
2014-09-15 03:48:58 -05:00
|
|
|
}
|
2014-09-17 16:33:31 -05:00
|
|
|
}
|