This allows deref patterns to move out of boxes. Implementation-wise, I've opted to put the information of whether a deref pattern uses a built-in deref or a method call in the THIR. It'd be a bit less code to check `.is_box()` everywhere, but I think this way feels more robust (and we don't have a `mutability` field in the THIR that we ignore when the smart pointer's a box). I'm not sure about the naming (or using `ByRef`), though.
68 lines
1.6 KiB
Rust
68 lines
1.6 KiB
Rust
//@ 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 else { unreachable!() };
|
|
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 else { unreachable!() };
|
|
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 else { unreachable!() };
|
|
drop::<NoCopy>(x);
|
|
};
|
|
f();
|
|
|
|
let b = Box::new((NoCopy,));
|
|
let f = || {
|
|
// this should move out of the box rather than borrow.
|
|
let (x,) = b else { unreachable!() };
|
|
drop::<NoCopy>(x);
|
|
};
|
|
f();
|
|
}
|