mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Implement DerefImm for Rc and DerefImm/DerefMut for RefCell's Ref/RefMut.
This commit is contained in:
parent
3b125ff3bb
commit
52532d13a6
@ -15,7 +15,7 @@ use clone::{Clone, DeepClone};
|
||||
use cmp::Eq;
|
||||
use fmt;
|
||||
use kinds::{marker, Pod};
|
||||
use ops::Drop;
|
||||
use ops::{Deref, DerefMut, Drop};
|
||||
use option::{None, Option, Some};
|
||||
|
||||
/// A mutable memory location that admits only `Pod` data.
|
||||
@ -258,6 +258,13 @@ impl<'b, T> Ref<'b, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, T> Deref<T> for Ref<'b, T> {
|
||||
#[inline]
|
||||
fn deref<'a>(&'a self) -> &'a T {
|
||||
&self.parent.value
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps a mutable borrowed reference to a value in a `RefCell` box.
|
||||
pub struct RefMut<'b, T> {
|
||||
priv parent: &'b mut RefCell<T>
|
||||
@ -279,6 +286,20 @@ impl<'b, T> RefMut<'b, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, T> Deref<T> for RefMut<'b, T> {
|
||||
#[inline]
|
||||
fn deref<'a>(&'a self) -> &'a T {
|
||||
&self.parent.value
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, T> DerefMut<T> for RefMut<'b, T> {
|
||||
#[inline]
|
||||
fn deref_mut<'a>(&'a mut self) -> &'a mut T {
|
||||
&mut self.parent.value
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
@ -27,7 +27,7 @@ use cast::transmute;
|
||||
use clone::{Clone, DeepClone};
|
||||
use cmp::{Eq, Ord};
|
||||
use kinds::marker;
|
||||
use ops::Drop;
|
||||
use ops::{Deref, Drop};
|
||||
use option::{Option, Some, None};
|
||||
use ptr;
|
||||
use rt::global_heap::exchange_free;
|
||||
@ -78,6 +78,14 @@ impl<T> Rc<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref<T> for Rc<T> {
|
||||
/// Borrow the value contained in the reference-counted box
|
||||
#[inline(always)]
|
||||
fn deref<'a>(&'a self) -> &'a T {
|
||||
unsafe { &(*self.ptr).value }
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe_destructor]
|
||||
impl<T> Drop for Rc<T> {
|
||||
fn drop(&mut self) {
|
||||
|
Loading…
Reference in New Issue
Block a user