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