编者注:此代码不再在 Rust 1.0 中编译并出现错误
parameter `'a` is never used
。此错误的原因正是由于下面演示的问题,因此(更新的)解决方案仍然适用。
extern crate core;
use core::ops::{Deref, DerefMut};
struct MutPtr<'a, T> {
ptr: *mut T,
}
impl<'a, T> MutPtr<'a, T> {
fn new<'b>(value: &'b mut T) -> MutPtr<'b, T> {
MutPtr { ptr: value }
}
}
impl<'a, T> Deref for MutPtr<'a, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &(*self.ptr) }
}
}
impl<'a, T> DerefMut for MutPtr<'a, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut (*self.ptr) }
}
}
struct Bar {
v: i32,
}
fn err<'a>() -> MutPtr<'a, Bar> {
let mut b = Bar { v: 42 };
MutPtr::new(&mut b) // Shouldn't this throw an error?
}
fn main() {
let mut b = Bar { v: 42 };
let mut ptr_b = MutPtr::new(&mut b);
let mut ptr_b1 = MutPtr::new(&mut b);
ptr_b.v = 10;
println!("{}", b.v);
ptr_b1.v = 21;
println!("{}", b.v);
}
此代码块引起了一些混乱:
fn err<'a>() -> MutPtr<'a, Bar> {
let mut b = Bar { v: 42 };
MutPtr::new(&mut b) // Shouldn't this throw an error?
}
为什么会这样编译?
当我打电话
MutPtr::new(&mut b)
它不应该有生命周期b
吗?我预计会出现编译错误,因为生命周期'a
比MutPtr<'b, Bar>
.