2014-09-15 08:48:58 +00:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// Test slicing sugar.
|
|
|
|
|
2014-09-26 05:48:16 +00:00
|
|
|
#![feature(slicing_syntax)]
|
|
|
|
|
2014-09-15 08:48:58 +00:00
|
|
|
extern crate core;
|
2015-01-03 00:34:13 +00:00
|
|
|
use core::ops::{Index, Range, RangeTo, RangeFrom, FullRange};
|
2014-09-15 08:48:58 +00:00
|
|
|
|
|
|
|
static mut COUNT: uint = 0;
|
|
|
|
|
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl Slice<Foo, Foo> for Foo {
|
2014-10-02 18:47:58 +00:00
|
|
|
fn as_slice_<'a>(&'a self) -> &'a Foo {
|
2014-09-15 08:48:58 +00:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2014-10-04 22:59:10 +00:00
|
|
|
fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
|
2014-09-15 08:48:58 +00:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2014-10-04 22:59:10 +00:00
|
|
|
fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
|
2014-09-15 08:48:58 +00:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2014-10-04 22:59:10 +00:00
|
|
|
fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
|
2014-09-15 08:48:58 +00:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SliceMut<Foo, Foo> for Foo {
|
2014-10-02 18:47:58 +00:00
|
|
|
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
|
2014-09-15 08:48:58 +00:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2014-10-04 22:59:10 +00:00
|
|
|
fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
|
2014-09-15 08:48:58 +00:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2014-10-04 22:59:10 +00:00
|
|
|
fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
|
2014-09-15 08:48:58 +00:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
2014-10-04 22:59:10 +00:00
|
|
|
fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
|
2014-09-15 08:48:58 +00:00
|
|
|
unsafe { COUNT += 1; }
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 00:34:13 +00:00
|
|
|
|
2014-09-15 08:48:58 +00:00
|
|
|
fn main() {
|
|
|
|
let mut x = Foo;
|
2015-01-03 00:34:13 +00:00
|
|
|
&x[];
|
|
|
|
&x[Foo..];
|
|
|
|
&x[0..Foo];
|
|
|
|
&x[Foo..Foo];
|
|
|
|
&mut x[];
|
|
|
|
&mut x[Foo..];
|
|
|
|
&mut x[..Foo];
|
|
|
|
&mut x[Foo..Foo];
|
2014-09-15 08:48:58 +00:00
|
|
|
unsafe {
|
|
|
|
assert!(COUNT == 8);
|
|
|
|
}
|
2014-09-17 21:33:31 +00:00
|
|
|
}
|