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