Implement DerefImm for Rc and DerefImm/DerefMut for RefCell's Ref/RefMut.

This commit is contained in:
Eduard Burtescu 2014-02-26 23:07:23 +02:00
parent 3b125ff3bb
commit 52532d13a6
2 changed files with 31 additions and 2 deletions

View File

@ -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::*;

View File

@ -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) {