]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | // Test that an `&mut self` method, when invoked on an lvalue whose | |
12 | // type is `&mut [u8]`, passes in a pointer to the lvalue and not a | |
13 | // temporary. Issue #19147. | |
14 | ||
c34b1796 | 15 | |
d9579d0f | 16 | #![feature(core)] |
c34b1796 | 17 | |
1a4d82fc | 18 | use std::slice; |
9346a6ac AL |
19 | |
20 | pub type IoResult<T> = Result<T, ()>; | |
1a4d82fc JJ |
21 | |
22 | trait MyWriter { | |
23 | fn my_write(&mut self, buf: &[u8]) -> IoResult<()>; | |
24 | } | |
25 | ||
26 | impl<'a> MyWriter for &'a mut [u8] { | |
27 | fn my_write(&mut self, buf: &[u8]) -> IoResult<()> { | |
c34b1796 | 28 | slice::bytes::copy_memory(buf, *self); |
1a4d82fc JJ |
29 | |
30 | let write_len = buf.len(); | |
31 | unsafe { | |
c34b1796 AL |
32 | *self = slice::from_raw_parts_mut( |
33 | self.as_mut_ptr().offset(write_len as isize), | |
34 | self.len() - write_len | |
35 | ); | |
1a4d82fc JJ |
36 | } |
37 | ||
38 | Ok(()) | |
39 | } | |
40 | } | |
41 | ||
42 | fn main() { | |
c34b1796 | 43 | let mut buf = [0; 6]; |
1a4d82fc JJ |
44 | |
45 | { | |
85aaf69f | 46 | let mut writer: &mut [_] = &mut buf; |
1a4d82fc JJ |
47 | writer.my_write(&[0, 1, 2]).unwrap(); |
48 | writer.my_write(&[3, 4, 5]).unwrap(); | |
49 | } | |
50 | ||
51 | // If `my_write` is not modifying `buf` in place, then we will | |
52 | // wind up with `[3, 4, 5, 0, 0, 0]` because the first call to | |
53 | // `my_write()` doesn't update the starting point for the write. | |
54 | ||
55 | assert_eq!(buf, [0, 1, 2, 3, 4, 5]); | |
56 | } |