2014-03-11 15:27:55 -05:00
|
|
|
// Test how overloaded deref interacts with borrows when DerefMut
|
|
|
|
// is implemented.
|
|
|
|
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
|
|
|
struct Own<T> {
|
|
|
|
value: *mut T
|
|
|
|
}
|
|
|
|
|
2015-01-01 13:53:20 -06:00
|
|
|
impl<T> Deref for Own<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
2014-07-17 23:44:59 -05:00
|
|
|
fn deref(&self) -> &T {
|
2014-03-11 15:27:55 -05:00
|
|
|
unsafe { &*self.value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 13:53:20 -06:00
|
|
|
impl<T> DerefMut for Own<T> {
|
2014-07-17 23:44:59 -05:00
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
2014-03-11 15:27:55 -05:00
|
|
|
unsafe { &mut *self.value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Point {
|
2015-01-08 04:54:35 -06:00
|
|
|
x: isize,
|
|
|
|
y: isize
|
2014-03-11 15:27:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Point {
|
2015-01-08 04:54:35 -06:00
|
|
|
fn get(&self) -> (isize, isize) {
|
2014-03-11 15:27:55 -05:00
|
|
|
(self.x, self.y)
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn set(&mut self, x: isize, y: isize) {
|
2014-03-11 15:27:55 -05:00
|
|
|
self.x = x;
|
|
|
|
self.y = y;
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn x_ref(&self) -> &isize {
|
2014-03-11 15:27:55 -05:00
|
|
|
&self.x
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn y_mut(&mut self) -> &mut isize {
|
2014-03-11 15:27:55 -05:00
|
|
|
&mut self.y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deref_imm_field(x: Own<Point>) {
|
2015-02-18 04:42:01 -06:00
|
|
|
let __isize = &x.y;
|
2014-03-11 15:27:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deref_mut_field1(x: Own<Point>) {
|
2015-02-18 04:42:01 -06:00
|
|
|
let __isize = &mut x.y; //~ ERROR cannot borrow
|
2014-03-11 15:27:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deref_mut_field2(mut x: Own<Point>) {
|
2015-02-18 04:42:01 -06:00
|
|
|
let __isize = &mut x.y;
|
2014-03-11 15:27:55 -05:00
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn deref_extend_field(x: &Own<Point>) -> &isize {
|
2014-03-11 15:27:55 -05:00
|
|
|
&x.y
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn deref_extend_mut_field1(x: &Own<Point>) -> &mut isize {
|
2014-03-11 15:27:55 -05:00
|
|
|
&mut x.y //~ ERROR cannot borrow
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn deref_extend_mut_field2(x: &mut Own<Point>) -> &mut isize {
|
2014-03-11 15:27:55 -05:00
|
|
|
&mut x.y
|
|
|
|
}
|
|
|
|
|
2014-07-17 23:44:59 -05:00
|
|
|
fn deref_extend_mut_field3(x: &mut Own<Point>) {
|
2014-05-05 20:56:44 -05:00
|
|
|
// Hmm, this is unfortunate, because with box it would work,
|
2014-03-12 12:31:00 -05:00
|
|
|
// but it's presently the expected outcome. See `deref_extend_mut_field4`
|
|
|
|
// for the workaround.
|
|
|
|
|
|
|
|
let _x = &mut x.x;
|
|
|
|
let _y = &mut x.y; //~ ERROR cannot borrow
|
2018-11-05 06:07:51 -06:00
|
|
|
use_mut(_x);
|
2014-03-12 12:31:00 -05:00
|
|
|
}
|
|
|
|
fn deref_extend_mut_field4<'a>(x: &'a mut Own<Point>) {
|
|
|
|
let p = &mut **x;
|
|
|
|
let _x = &mut p.x;
|
|
|
|
let _y = &mut p.y;
|
|
|
|
}
|
|
|
|
|
2014-03-11 15:27:55 -05:00
|
|
|
fn assign_field1<'a>(x: Own<Point>) {
|
|
|
|
x.y = 3; //~ ERROR cannot borrow
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assign_field2<'a>(x: &'a Own<Point>) {
|
2016-05-24 06:37:11 -05:00
|
|
|
x.y = 3; //~ ERROR cannot borrow
|
2014-03-11 15:27:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn assign_field3<'a>(x: &'a mut Own<Point>) {
|
|
|
|
x.y = 3;
|
|
|
|
}
|
|
|
|
|
2014-03-12 12:31:00 -05:00
|
|
|
fn assign_field4<'a>(x: &'a mut Own<Point>) {
|
|
|
|
let _p: &mut Point = &mut **x;
|
|
|
|
x.y = 3; //~ ERROR cannot borrow
|
2018-11-05 06:07:51 -06:00
|
|
|
use_mut(_p);
|
2014-03-12 12:31:00 -05:00
|
|
|
}
|
2014-03-11 15:27:55 -05:00
|
|
|
fn deref_imm_method(x: Own<Point>) {
|
2015-02-18 04:42:01 -06:00
|
|
|
let __isize = x.get();
|
2014-03-11 15:27:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deref_mut_method1(x: Own<Point>) {
|
|
|
|
x.set(0, 0); //~ ERROR cannot borrow
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deref_mut_method2(mut x: Own<Point>) {
|
|
|
|
x.set(0, 0);
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn deref_extend_method(x: &Own<Point>) -> &isize {
|
2014-03-11 15:27:55 -05:00
|
|
|
x.x_ref()
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn deref_extend_mut_method1(x: &Own<Point>) -> &mut isize {
|
2014-03-11 15:27:55 -05:00
|
|
|
x.y_mut() //~ ERROR cannot borrow
|
|
|
|
}
|
|
|
|
|
2015-01-08 04:54:35 -06:00
|
|
|
fn deref_extend_mut_method2(x: &mut Own<Point>) -> &mut isize {
|
2014-03-11 15:27:55 -05:00
|
|
|
x.y_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assign_method1<'a>(x: Own<Point>) {
|
|
|
|
*x.y_mut() = 3; //~ ERROR cannot borrow
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assign_method2<'a>(x: &'a Own<Point>) {
|
|
|
|
*x.y_mut() = 3; //~ ERROR cannot borrow
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assign_method3<'a>(x: &'a mut Own<Point>) {
|
|
|
|
*x.y_mut() = 3;
|
|
|
|
}
|
|
|
|
|
2014-03-12 12:31:00 -05:00
|
|
|
pub fn main() {}
|
2018-11-05 06:07:51 -06:00
|
|
|
|
|
|
|
fn use_mut<T>(_: &mut T) {}
|