1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
//@ run-pass
#![feature(deref_patterns)]
#![allow(incomplete_features)]
use std::rc::Rc;
struct NoCopy;
fn main() {
let b = Rc::new("aaa".to_string());
let f = || {
let deref!(ref s) = b;
assert_eq!(s.len(), 3);
};
assert_eq!(b.len(), 3);
f();
let v = vec![1, 2, 3];
let f = || {
// this should count as a borrow of `v` as a whole
let [.., x] = v else { unreachable!() };
assert_eq!(x, 3);
};
assert_eq!(v, [1, 2, 3]);
f();
let mut b = "aaa".to_string();
let mut f = || {
let deref!(ref mut s) = b;
s.make_ascii_uppercase();
};
f();
assert_eq!(b, "AAA");
let mut v = vec![1, 2, 3];
let mut f = || {
// this should count as a mutable borrow of `v` as a whole
let [.., ref mut x] = v else { unreachable!() };
*x = 4;
};
f();
assert_eq!(v, [1, 2, 4]);
let mut v = vec![1, 2, 3];
let mut f = || {
// here, `[.., x]` is adjusted by both an overloaded deref and a builtin deref
let [.., x] = &mut v else { unreachable!() };
*x = 4;
};
f();
assert_eq!(v, [1, 2, 4]);
let b = Box::new(NoCopy);
let f = || {
// this should move out of the box rather than borrow.
let deref!(x) = b;
drop::<NoCopy>(x);
};
f();
let b = Box::new((NoCopy,));
let f = || {
// this should move out of the box rather than borrow.
let (x,) = b;
drop::<NoCopy>(x);
};
f();
}
|