core: use assoc types in `Deref[Mut]`

This commit is contained in:
Jorge Aparicio 2015-01-01 14:53:20 -05:00
parent d555772554
commit 64b7c22c46
49 changed files with 256 additions and 107 deletions

View File

@ -247,7 +247,9 @@ impl<T> BorrowFrom<Arc<T>> for T {
}
#[experimental = "Deref is experimental."]
impl<T> Deref<T> for Arc<T> {
impl<T> Deref for Arc<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&self.inner().data

View File

@ -153,11 +153,13 @@ impl fmt::Show for Box<Any+'static> {
}
}
impl<Sized? T> Deref<T> for Box<T> {
impl<Sized? T> Deref for Box<T> {
type Target = T;
fn deref(&self) -> &T { &**self }
}
impl<Sized? T> DerefMut<T> for Box<T> {
impl<Sized? T> DerefMut for Box<T> {
fn deref_mut(&mut self) -> &mut T { &mut **self }
}
@ -210,7 +212,7 @@ mod test {
#[test]
fn deref() {
fn homura<T: Deref<i32>>(_: T) { }
fn homura<T: Deref<Target=i32>>(_: T) { }
homura(box 765i32);
}
}

View File

@ -65,6 +65,7 @@
#![no_std]
#![feature(lang_items, phase, unsafe_destructor, default_type_params)]
#![feature(associated_types)]
#[phase(plugin, link)]
extern crate core;

View File

@ -355,7 +355,9 @@ impl<T> BorrowFrom<Rc<T>> for T {
}
#[experimental = "Deref is experimental."]
impl<T> Deref<T> for Rc<T> {
impl<T> Deref for Rc<T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &T {
&self.inner().value

View File

@ -515,13 +515,15 @@ mod stack {
marker: marker::InvariantLifetime<'id>
}
impl<'id, T> Deref<T> for IdRef<'id, T> {
impl<'id, T> Deref for IdRef<'id, T> {
type Target = T;
fn deref(&self) -> &T {
&*self.inner
}
}
impl<'id, T> DerefMut<T> for IdRef<'id, T> {
impl<'id, T> DerefMut for IdRef<'id, T> {
fn deref_mut(&mut self) -> &mut T {
&mut *self.inner
}

View File

@ -455,7 +455,9 @@ impl<K: Clone, V: Clone> Clone for Node<K, V> {
/// flag: &'a Cell<bool>,
/// }
///
/// impl<'a> Deref<Node<uint, uint>> for Nasty<'a> {
/// impl<'a> Deref for Nasty<'a> {
/// type Target = Node<uint, uint>;
///
/// fn deref(&self) -> &Node<uint, uint> {
/// if self.flag.get() {
/// &*self.second
@ -511,7 +513,7 @@ impl<K: Ord, V> Node<K, V> {
/// Searches for the given key in the node. If it finds an exact match,
/// `Found` will be yielded with the matching index. If it doesn't find an exact match,
/// `GoDown` will be yielded with the index of the subtree the key must lie in.
pub fn search<Sized? Q, NodeRef: Deref<Node<K, V>>>(node: NodeRef, key: &Q)
pub fn search<Sized? Q, NodeRef: Deref<Target=Node<K, V>>>(node: NodeRef, key: &Q)
-> SearchResult<NodeRef> where Q: BorrowFrom<K> + Ord {
// FIXME(Gankro): Tune when to search linear or binary based on B (and maybe K/V).
// For the B configured as of this writing (B = 6), binary search was *significantly*
@ -588,7 +590,7 @@ impl <K, V> Node<K, V> {
}
}
impl<K, V, NodeRef: Deref<Node<K, V>>, Type, NodeType> Handle<NodeRef, Type, NodeType> {
impl<K, V, NodeRef: Deref<Target=Node<K, V>>, Type, NodeType> Handle<NodeRef, Type, NodeType> {
/// Returns a reference to the node that contains the pointed-to edge or key/value pair. This
/// is very different from `edge` and `edge_mut` because those return children of the node
/// returned by `node`.
@ -597,7 +599,9 @@ impl<K, V, NodeRef: Deref<Node<K, V>>, Type, NodeType> Handle<NodeRef, Type, Nod
}
}
impl<K, V, NodeRef: DerefMut<Node<K, V>>, Type, NodeType> Handle<NodeRef, Type, NodeType> {
impl<K, V, NodeRef, Type, NodeType> Handle<NodeRef, Type, NodeType> where
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
{
/// Converts a handle into one that stores the same information using a raw pointer. This can
/// be useful in conjunction with `from_raw` when the type system is insufficient for
/// determining the lifetimes of the nodes.
@ -653,7 +657,7 @@ impl<'a, K: 'a, V: 'a> Handle<&'a mut Node<K, V>, handle::Edge, handle::Internal
}
}
impl<K, V, NodeRef: Deref<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Internal> {
impl<K, V, NodeRef: Deref<Target=Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Internal> {
// This doesn't exist because there are no uses for it,
// but is fine to add, analagous to edge_mut.
//
@ -667,7 +671,7 @@ pub enum ForceResult<NodeRef, Type> {
Internal(Handle<NodeRef, Type, handle::Internal>)
}
impl<K, V, NodeRef: Deref<Node<K, V>>, Type> Handle<NodeRef, Type, handle::LeafOrInternal> {
impl<K, V, NodeRef: Deref<Target=Node<K, V>>, Type> Handle<NodeRef, Type, handle::LeafOrInternal> {
/// Figure out whether this handle is pointing to something in a leaf node or to something in
/// an internal node, clarifying the type according to the result.
pub fn force(self) -> ForceResult<NodeRef, Type> {
@ -684,8 +688,9 @@ impl<K, V, NodeRef: Deref<Node<K, V>>, Type> Handle<NodeRef, Type, handle::LeafO
}
}
}
impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Leaf> {
impl<K, V, NodeRef> Handle<NodeRef, handle::Edge, handle::Leaf> where
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
{
/// Tries to insert this key-value pair at the given index in this leaf node
/// If the node is full, we have to split it.
///
@ -717,7 +722,9 @@ impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::
}
}
impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Internal> {
impl<K, V, NodeRef> Handle<NodeRef, handle::Edge, handle::Internal> where
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
{
/// Returns a mutable reference to the edge pointed-to by this handle. This should not be
/// confused with `node`, which references the parent node of what is returned here.
pub fn edge_mut(&mut self) -> &mut Node<K, V> {
@ -800,7 +807,9 @@ impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::
}
}
impl<K, V, NodeRef: DerefMut<Node<K, V>>, NodeType> Handle<NodeRef, handle::Edge, NodeType> {
impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::Edge, NodeType> where
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
{
/// Gets the handle pointing to the key/value pair just to the left of the pointed-to edge.
/// This is unsafe because the handle might point to the first edge in the node, which has no
/// pair to its left.
@ -862,7 +871,7 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<&'a mut Node<K, V>, handle::KV, NodeType
}
}
impl<'a, K: 'a, V: 'a, NodeRef: Deref<Node<K, V>> + 'a, NodeType> Handle<NodeRef, handle::KV,
impl<'a, K: 'a, V: 'a, NodeRef: Deref<Target=Node<K, V>> + 'a, NodeType> Handle<NodeRef, handle::KV,
NodeType> {
// These are fine to include, but are currently unneeded.
//
@ -881,8 +890,9 @@ impl<'a, K: 'a, V: 'a, NodeRef: Deref<Node<K, V>> + 'a, NodeType> Handle<NodeRef
// }
}
impl<'a, K: 'a, V: 'a, NodeRef: DerefMut<Node<K, V>> + 'a, NodeType> Handle<NodeRef, handle::KV,
NodeType> {
impl<'a, K: 'a, V: 'a, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
NodeRef: 'a + Deref<Target=Node<K, V>> + DerefMut,
{
/// Returns a mutable reference to the key pointed-to by this handle. This doesn't return a
/// reference with a lifetime as large as `into_kv_mut`, but it also does not consume the
/// handle.
@ -898,7 +908,9 @@ impl<'a, K: 'a, V: 'a, NodeRef: DerefMut<Node<K, V>> + 'a, NodeType> Handle<Node
}
}
impl<K, V, NodeRef: DerefMut<Node<K, V>>, NodeType> Handle<NodeRef, handle::KV, NodeType> {
impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
{
/// Gets the handle pointing to the edge immediately to the left of the key/value pair pointed
/// to by this handle.
pub fn left_edge<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::Edge, NodeType> {
@ -918,7 +930,9 @@ impl<K, V, NodeRef: DerefMut<Node<K, V>>, NodeType> Handle<NodeRef, handle::KV,
}
}
impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::KV, handle::Leaf> {
impl<K, V, NodeRef> Handle<NodeRef, handle::KV, handle::Leaf> where
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
{
/// Removes the key/value pair at the handle's location.
///
/// # Panics (in debug build)
@ -929,7 +943,9 @@ impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::KV, handle::Le
}
}
impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::KV, handle::Internal> {
impl<K, V, NodeRef> Handle<NodeRef, handle::KV, handle::Internal> where
NodeRef: Deref<Target=Node<K, V>> + DerefMut
{
/// Steal! Stealing is roughly analogous to a binary tree rotation.
/// In this case, we're "rotating" right.
unsafe fn steal_rightward(&mut self) {

View File

@ -935,7 +935,9 @@ impl ops::Slice<uint, str> for String {
}
#[experimental = "waiting on Deref stabilization"]
impl ops::Deref<str> for String {
impl ops::Deref for String {
type Target = str;
fn deref<'a>(&'a self) -> &'a str {
unsafe { mem::transmute(self.vec[]) }
}
@ -947,7 +949,9 @@ pub struct DerefString<'a> {
x: DerefVec<'a, u8>
}
impl<'a> Deref<String> for DerefString<'a> {
impl<'a> Deref for DerefString<'a> {
type Target = String;
fn deref<'b>(&'b self) -> &'b String {
unsafe { mem::transmute(&*self.x) }
}

View File

@ -1301,12 +1301,14 @@ impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
}
#[experimental = "waiting on Deref stability"]
impl<T> ops::Deref<[T]> for Vec<T> {
impl<T> ops::Deref for Vec<T> {
type Target = [T];
fn deref<'a>(&'a self) -> &'a [T] { self.as_slice() }
}
#[experimental = "waiting on DerefMut stability"]
impl<T> ops::DerefMut<[T]> for Vec<T> {
impl<T> ops::DerefMut for Vec<T> {
fn deref_mut<'a>(&'a mut self) -> &'a mut [T] { self.as_mut_slice() }
}
@ -1716,7 +1718,9 @@ pub struct DerefVec<'a, T> {
}
#[experimental]
impl<'a, T> Deref<Vec<T>> for DerefVec<'a, T> {
impl<'a, T> Deref for DerefVec<'a, T> {
type Target = Vec<T>;
fn deref<'b>(&'b self) -> &'b Vec<T> {
&self.x
}

View File

@ -54,7 +54,7 @@ macro_rules! array_impls {
#[stable]
impl<'a, A, B, Rhs> PartialEq<Rhs> for [A; $N] where
A: PartialEq<B>,
Rhs: Deref<[B]>,
Rhs: Deref<Target=[B]>,
{
#[inline(always)]
fn eq(&self, other: &Rhs) -> bool { PartialEq::eq(self[], &**other) }
@ -65,7 +65,7 @@ macro_rules! array_impls {
#[stable]
impl<'a, A, B, Lhs> PartialEq<[B; $N]> for Lhs where
A: PartialEq<B>,
Lhs: Deref<[A]>
Lhs: Deref<Target=[A]>
{
#[inline(always)]
fn eq(&self, other: &[B; $N]) -> bool { PartialEq::eq(&**self, other[]) }

View File

@ -191,7 +191,9 @@ impl<'a, T, Sized? B> Cow<'a, T, B> where B: ToOwned<T> {
}
}
impl<'a, T, Sized? B> Deref<B> for Cow<'a, T, B> where B: ToOwned<T> {
impl<'a, T, Sized? B> Deref for Cow<'a, T, B> where B: ToOwned<T> {
type Target = B;
fn deref(&self) -> &B {
match *self {
Borrowed(borrowed) => borrowed,

View File

@ -422,7 +422,9 @@ pub struct Ref<'b, T:'b> {
}
#[unstable = "waiting for `Deref` to become stable"]
impl<'b, T> Deref<T> for Ref<'b, T> {
impl<'b, T> Deref for Ref<'b, T> {
type Target = T;
#[inline]
fn deref<'a>(&'a self) -> &'a T {
self._value
@ -478,7 +480,9 @@ pub struct RefMut<'b, T:'b> {
}
#[unstable = "waiting for `Deref` to become stable"]
impl<'b, T> Deref<T> for RefMut<'b, T> {
impl<'b, T> Deref for RefMut<'b, T> {
type Target = T;
#[inline]
fn deref<'a>(&'a self) -> &'a T {
self._value
@ -486,7 +490,7 @@ impl<'b, T> Deref<T> for RefMut<'b, T> {
}
#[unstable = "waiting for `DerefMut` to become stable"]
impl<'b, T> DerefMut<T> for RefMut<'b, T> {
impl<'b, T> DerefMut for RefMut<'b, T> {
#[inline]
fn deref_mut<'a>(&'a mut self) -> &'a mut T {
self._value

View File

@ -1174,7 +1174,7 @@ pub trait IteratorCloneExt<A> {
}
#[unstable = "trait is unstable"]
impl<A: Clone, D: Deref<A>, I: Iterator<D>> IteratorCloneExt<A> for I {
impl<A: Clone, D: Deref<Target=A>, I: Iterator<D>> IteratorCloneExt<A> for I {
fn cloned(self) -> Cloned<I> {
Cloned { it: self }
}
@ -1185,7 +1185,7 @@ pub struct Cloned<I> {
it: I,
}
impl<A: Clone, D: Deref<A>, I: Iterator<D>> Iterator<A> for Cloned<I> {
impl<A: Clone, D: Deref<Target=A>, I: Iterator<D>> Iterator<A> for Cloned<I> {
fn next(&mut self) -> Option<A> {
self.it.next().cloned()
}
@ -1195,7 +1195,7 @@ impl<A: Clone, D: Deref<A>, I: Iterator<D>> Iterator<A> for Cloned<I> {
}
}
impl<A: Clone, D: Deref<A>, I: DoubleEndedIterator<D>>
impl<A: Clone, D: Deref<Target=A>, I: DoubleEndedIterator<D>>
DoubleEndedIterator<A> for Cloned<I> {
fn next_back(&mut self) -> Option<A> {
self.it.next_back().cloned()
@ -1203,7 +1203,7 @@ impl<A: Clone, D: Deref<A>, I: DoubleEndedIterator<D>>
}
#[unstable = "trait is unstable"]
impl<A: Clone, D: Deref<A>, I: ExactSizeIterator<D>> ExactSizeIterator<A> for Cloned<I> {}
impl<A: Clone, D: Deref<Target=A>, I: ExactSizeIterator<D>> ExactSizeIterator<A> for Cloned<I> {}
#[unstable = "recently renamed for extension trait conventions"]
/// An extension trait for cloneable iterators.

View File

@ -44,7 +44,9 @@ impl<T: Zeroable> NonZero<T> {
}
}
impl<T: Zeroable> Deref<T> for NonZero<T> {
impl<T: Zeroable> Deref for NonZero<T> {
type Target = T;
#[inline]
fn deref<'a>(&'a self) -> &'a T {
let NonZero(ref inner) = *self;

View File

@ -827,11 +827,14 @@ pub struct RangeTo<Idx> {
/// struct.
///
/// ```
/// #![feature(associated_types)]
/// struct DerefExample<T> {
/// value: T
/// }
///
/// impl<T> Deref<T> for DerefExample<T> {
/// impl<T> Deref for DerefExample<T> {
/// type Target = T;
///
/// fn deref<'a>(&'a self) -> &'a T {
/// &self.value
/// }
@ -843,16 +846,22 @@ pub struct RangeTo<Idx> {
/// }
/// ```
#[lang="deref"]
pub trait Deref<Sized? Result> for Sized? {
pub trait Deref for Sized? {
type Sized? Target;
/// The method called to dereference a value
fn deref<'a>(&'a self) -> &'a Result;
fn deref<'a>(&'a self) -> &'a Self::Target;
}
impl<'a, Sized? T> Deref<T> for &'a T {
impl<'a, Sized? T> Deref for &'a T {
type Target = T;
fn deref(&self) -> &T { *self }
}
impl<'a, Sized? T> Deref<T> for &'a mut T {
impl<'a, Sized? T> Deref for &'a mut T {
type Target = T;
fn deref(&self) -> &T { *self }
}
@ -865,17 +874,20 @@ impl<'a, Sized? T> Deref<T> for &'a mut T {
/// struct.
///
/// ```
/// #![feature(associated_types)]
/// struct DerefMutExample<T> {
/// value: T
/// }
///
/// impl<T> Deref<T> for DerefMutExample<T> {
/// impl<T> Deref for DerefMutExample<T> {
/// type Target = T;
///
/// fn deref<'a>(&'a self) -> &'a T {
/// &self.value
/// }
/// }
///
/// impl<T> DerefMut<T> for DerefMutExample<T> {
/// impl<T> DerefMut for DerefMutExample<T> {
/// fn deref_mut<'a>(&'a mut self) -> &'a mut T {
/// &mut self.value
/// }
@ -888,12 +900,12 @@ impl<'a, Sized? T> Deref<T> for &'a mut T {
/// }
/// ```
#[lang="deref_mut"]
pub trait DerefMut<Sized? Result> for Sized? : Deref<Result> {
pub trait DerefMut for Sized? : Deref {
/// The method called to mutably dereference a value
fn deref_mut<'a>(&'a mut self) -> &'a mut Result;
fn deref_mut<'a>(&'a mut self) -> &'a mut <Self as Deref>::Target;
}
impl<'a, Sized? T> DerefMut<T> for &'a mut T {
impl<'a, Sized? T> DerefMut for &'a mut T {
fn deref_mut(&mut self) -> &mut T { *self }
}

View File

@ -699,7 +699,7 @@ impl<T> Option<T> {
}
}
impl<'a, T: Clone, D: Deref<T>> Option<D> {
impl<'a, T: Clone, D: Deref<Target=T>> Option<D> {
/// Maps an Option<D> to an Option<T> by dereffing and cloning the contents of the Option.
/// Useful for converting an Option<&T> to an Option<T>.
#[unstable = "recently added as part of collections reform"]

View File

@ -95,13 +95,15 @@ struct GraphBuilder<'a, 'b:'a, 'tcx:'b> {
resolver: &'a mut Resolver<'b, 'tcx>
}
impl<'a, 'b:'a, 'tcx:'b> Deref<Resolver<'b, 'tcx>> for GraphBuilder<'a, 'b, 'tcx> {
impl<'a, 'b:'a, 'tcx:'b> Deref for GraphBuilder<'a, 'b, 'tcx> {
type Target = Resolver<'b, 'tcx>;
fn deref(&self) -> &Resolver<'b, 'tcx> {
&*self.resolver
}
}
impl<'a, 'b:'a, 'tcx:'b> DerefMut<Resolver<'b, 'tcx>> for GraphBuilder<'a, 'b, 'tcx> {
impl<'a, 'b:'a, 'tcx:'b> DerefMut for GraphBuilder<'a, 'b, 'tcx> {
fn deref_mut(&mut self) -> &mut Resolver<'b, 'tcx> {
&mut *self.resolver
}

View File

@ -33,13 +33,15 @@ struct UnusedImportCheckVisitor<'a, 'b:'a, 'tcx:'b> {
}
// Deref and DerefMut impls allow treating UnusedImportCheckVisitor as Resolver.
impl<'a, 'b, 'tcx:'b> Deref<Resolver<'b, 'tcx>> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
impl<'a, 'b, 'tcx:'b> Deref for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
type Target = Resolver<'b, 'tcx>;
fn deref<'c>(&'c self) -> &'c Resolver<'b, 'tcx> {
&*self.resolver
}
}
impl<'a, 'b, 'tcx:'b> DerefMut<Resolver<'b, 'tcx>> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
impl<'a, 'b, 'tcx:'b> DerefMut for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
fn deref_mut<'c>(&'c mut self) -> &'c mut Resolver<'b, 'tcx> {
&mut *self.resolver
}

View File

@ -18,6 +18,7 @@
#![feature(globs, phase, slicing_syntax)]
#![feature(rustc_diagnostic_macros)]
#![feature(associated_types)]
#[phase(plugin, link)] extern crate log;
#[phase(plugin, link)] extern crate syntax;

View File

@ -34,13 +34,15 @@ struct ExportRecorder<'a, 'b:'a, 'tcx:'b> {
}
// Deref and DerefMut impls allow treating ExportRecorder as Resolver.
impl<'a, 'b, 'tcx:'b> Deref<Resolver<'b, 'tcx>> for ExportRecorder<'a, 'b, 'tcx> {
impl<'a, 'b, 'tcx:'b> Deref for ExportRecorder<'a, 'b, 'tcx> {
type Target = Resolver<'b, 'tcx>;
fn deref<'c>(&'c self) -> &'c Resolver<'b, 'tcx> {
&*self.resolver
}
}
impl<'a, 'b, 'tcx:'b> DerefMut<Resolver<'b, 'tcx>> for ExportRecorder<'a, 'b, 'tcx> {
impl<'a, 'b, 'tcx:'b> DerefMut for ExportRecorder<'a, 'b, 'tcx> {
fn deref_mut<'c>(&'c mut self) -> &'c mut Resolver<'b, 'tcx> {
&mut *self.resolver
}

View File

@ -311,7 +311,7 @@ fn search_hashed<K, V, M, F>(table: M,
hash: SafeHash,
mut is_match: F)
-> SearchResult<K, V, M> where
M: Deref<RawTable<K, V>>,
M: Deref<Target=RawTable<K, V>>,
F: FnMut(&K) -> bool,
{
let size = table.size();

View File

@ -210,7 +210,7 @@ impl<K, V, M> Bucket<K, V, M> {
}
}
impl<K, V, M: Deref<RawTable<K, V>>> Bucket<K, V, M> {
impl<K, V, M: Deref<Target=RawTable<K, V>>> Bucket<K, V, M> {
pub fn new(table: M, hash: SafeHash) -> Bucket<K, V, M> {
Bucket::at_index(table, hash.inspect() as uint)
}
@ -279,7 +279,7 @@ impl<K, V, M: Deref<RawTable<K, V>>> Bucket<K, V, M> {
}
}
impl<K, V, M: Deref<RawTable<K, V>>> EmptyBucket<K, V, M> {
impl<K, V, M: Deref<Target=RawTable<K, V>>> EmptyBucket<K, V, M> {
#[inline]
pub fn next(self) -> Bucket<K, V, M> {
let mut bucket = self.into_bucket();
@ -315,7 +315,7 @@ impl<K, V, M: Deref<RawTable<K, V>>> EmptyBucket<K, V, M> {
}
}
impl<K, V, M: DerefMut<RawTable<K, V>>> EmptyBucket<K, V, M> {
impl<K, V, M: Deref<Target=RawTable<K, V>> + DerefMut> EmptyBucket<K, V, M> {
/// Puts given key and value pair, along with the key's hash,
/// into this bucket in the hashtable. Note how `self` is 'moved' into
/// this function, because this slot will no longer be empty when
@ -337,7 +337,7 @@ impl<K, V, M: DerefMut<RawTable<K, V>>> EmptyBucket<K, V, M> {
}
}
impl<K, V, M: Deref<RawTable<K, V>>> FullBucket<K, V, M> {
impl<K, V, M: Deref<Target=RawTable<K, V>>> FullBucket<K, V, M> {
#[inline]
pub fn next(self) -> Bucket<K, V, M> {
let mut bucket = self.into_bucket();
@ -384,7 +384,7 @@ impl<K, V, M: Deref<RawTable<K, V>>> FullBucket<K, V, M> {
}
}
impl<K, V, M: DerefMut<RawTable<K, V>>> FullBucket<K, V, M> {
impl<K, V, M: Deref<Target=RawTable<K, V>> + DerefMut> FullBucket<K, V, M> {
/// Removes this bucket's key and value from the hashtable.
///
/// This works similarly to `put`, building an `EmptyBucket` out of the
@ -428,7 +428,7 @@ impl<K, V, M: DerefMut<RawTable<K, V>>> FullBucket<K, V, M> {
}
}
impl<'t, K, V, M: Deref<RawTable<K, V>> + 't> FullBucket<K, V, M> {
impl<'t, K, V, M: Deref<Target=RawTable<K, V>> + 't> FullBucket<K, V, M> {
/// Exchange a bucket state for immutable references into the table.
/// Because the underlying reference to the table is also consumed,
/// no further changes to the structure of the table are possible;
@ -442,7 +442,7 @@ impl<'t, K, V, M: Deref<RawTable<K, V>> + 't> FullBucket<K, V, M> {
}
}
impl<'t, K, V, M: DerefMut<RawTable<K, V>> + 't> FullBucket<K, V, M> {
impl<'t, K, V, M: Deref<Target=RawTable<K, V>> + DerefMut + 't> FullBucket<K, V, M> {
/// This works similarly to `into_refs`, exchanging a bucket state
/// for mutable references into the table.
pub fn into_mut_refs(self) -> (&'t mut K, &'t mut V) {
@ -463,7 +463,7 @@ impl<K, V, M> BucketState<K, V, M> {
}
}
impl<K, V, M: Deref<RawTable<K, V>>> GapThenFull<K, V, M> {
impl<K, V, M: Deref<Target=RawTable<K, V>>> GapThenFull<K, V, M> {
#[inline]
pub fn full(&self) -> &FullBucket<K, V, M> {
&self.full

View File

@ -117,13 +117,15 @@ pub struct StdinReaderGuard<'a> {
inner: MutexGuard<'a, RaceBox>,
}
impl<'a> Deref<BufferedReader<StdReader>> for StdinReaderGuard<'a> {
impl<'a> Deref for StdinReaderGuard<'a> {
type Target = BufferedReader<StdReader>;
fn deref(&self) -> &BufferedReader<StdReader> {
&self.inner.0
}
}
impl<'a> DerefMut<BufferedReader<StdReader>> for StdinReaderGuard<'a> {
impl<'a> DerefMut for StdinReaderGuard<'a> {
fn deref_mut(&mut self) -> &mut BufferedReader<StdReader> {
&mut self.inner.0
}

View File

@ -74,10 +74,12 @@ impl<'a, T: Send> ExclusiveGuard<'a, T> {
}
}
impl<'a, T: Send> Deref<T> for ExclusiveGuard<'a, T> {
impl<'a, T: Send> Deref for ExclusiveGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T { &*self._data }
}
impl<'a, T: Send> DerefMut<T> for ExclusiveGuard<'a, T> {
impl<'a, T: Send> DerefMut for ExclusiveGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T { &mut *self._data }
}

View File

@ -288,12 +288,14 @@ impl<'mutex, T> MutexGuard<'mutex, T> {
}
}
impl<'mutex, T> Deref<T> for MutexGuard<'mutex, T> {
impl<'mutex, T> Deref for MutexGuard<'mutex, T> {
type Target = T;
fn deref<'a>(&'a self) -> &'a T {
unsafe { &*self.__data.get() }
}
}
impl<'mutex, T> DerefMut<T> for MutexGuard<'mutex, T> {
impl<'mutex, T> DerefMut for MutexGuard<'mutex, T> {
fn deref_mut<'a>(&'a mut self) -> &'a mut T {
unsafe { &mut *self.__data.get() }
}

View File

@ -326,13 +326,17 @@ impl<'rwlock, T> RWLockWriteGuard<'rwlock, T> {
}
}
impl<'rwlock, T> Deref<T> for RWLockReadGuard<'rwlock, T> {
impl<'rwlock, T> Deref for RWLockReadGuard<'rwlock, T> {
type Target = T;
fn deref(&self) -> &T { unsafe { &*self.__data.get() } }
}
impl<'rwlock, T> Deref<T> for RWLockWriteGuard<'rwlock, T> {
impl<'rwlock, T> Deref for RWLockWriteGuard<'rwlock, T> {
type Target = T;
fn deref(&self) -> &T { unsafe { &*self.__data.get() } }
}
impl<'rwlock, T> DerefMut<T> for RWLockWriteGuard<'rwlock, T> {
impl<'rwlock, T> DerefMut for RWLockWriteGuard<'rwlock, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.__data.get() }
}

View File

@ -26,6 +26,7 @@
#![feature(macro_rules, globs, default_type_params, phase, slicing_syntax)]
#![feature(quote, unsafe_destructor)]
#![feature(unboxed_closures)]
#![feature(associated_types)]
extern crate arena;
extern crate fmt_macros;

View File

@ -54,7 +54,9 @@ impl<T> OwnedSlice<T> {
}
}
impl<T> Deref<[T]> for OwnedSlice<T> {
impl<T> Deref for OwnedSlice<T> {
type Target = [T];
fn deref(&self) -> &[T] {
self.as_slice()
}

View File

@ -606,7 +606,9 @@ impl InternedString {
}
}
impl Deref<str> for InternedString {
impl Deref for InternedString {
type Target = str;
fn deref(&self) -> &str { &*self.string }
}

View File

@ -75,7 +75,9 @@ impl<T: 'static> P<T> {
}
}
impl<T> Deref<T> for P<T> {
impl<T> Deref for P<T> {
type Target = T;
fn deref<'a>(&'a self) -> &'a T {
&*self.ptr
}

View File

@ -124,7 +124,9 @@ impl BorrowFrom<RcStr> for str {
}
}
impl Deref<str> for RcStr {
impl Deref for RcStr {
type Target = str;
fn deref(&self) -> &str { self.string[] }
}

View File

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(associated_types)]
use std::ops::Deref;
struct DerefWithHelper<H, T> {
@ -24,7 +26,9 @@ impl<T> Helper<T> for Option<T> {
}
}
impl<T, H: Helper<T>> Deref<T> for DerefWithHelper<H, T> {
impl<T, H: Helper<T>> Deref for DerefWithHelper<H, T> {
type Target = T;
fn deref(&self) -> &T {
self.helper.helper_borrow()
}

View File

@ -11,19 +11,23 @@
// Test how overloaded deref interacts with borrows when DerefMut
// is implemented.
#![feature(associated_types)]
use std::ops::{Deref, DerefMut};
struct Own<T> {
value: *mut T
}
impl<T> Deref<T> for Own<T> {
impl<T> Deref for Own<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.value }
}
}
impl<T> DerefMut<T> for Own<T> {
impl<T> DerefMut for Own<T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.value }
}

View File

@ -11,13 +11,17 @@
// Test how overloaded deref interacts with borrows when only
// Deref and not DerefMut is implemented.
#![feature(associated_types)]
use std::ops::Deref;
struct Rc<T> {
value: *const T
}
impl<T> Deref<T> for Rc<T> {
impl<T> Deref for Rc<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.value }
}

View File

@ -11,19 +11,23 @@
// Test how overloaded deref interacts with borrows when DerefMut
// is implemented.
#![feature(associated_types)]
use std::ops::{Deref, DerefMut};
struct Own<T> {
value: *mut T
}
impl<T> Deref<T> for Own<T> {
impl<T> Deref for Own<T> {
type Target = T;
fn deref<'a>(&'a self) -> &'a T {
unsafe { &*self.value }
}
}
impl<T> DerefMut<T> for Own<T> {
impl<T> DerefMut for Own<T> {
fn deref_mut<'a>(&'a mut self) -> &'a mut T {
unsafe { &mut *self.value }
}

View File

@ -11,13 +11,17 @@
// Test how overloaded deref interacts with borrows when only
// Deref and not DerefMut is implemented.
#![feature(associated_types)]
use std::ops::Deref;
struct Rc<T> {
value: *const T
}
impl<T> Deref<T> for Rc<T> {
impl<T> Deref for Rc<T> {
type Target = T;
fn deref<'a>(&'a self) -> &'a T {
unsafe { &*self.value }
}

View File

@ -10,11 +10,15 @@
// error-pattern: reached the recursion limit while auto-dereferencing
#![feature(associated_types)]
use std::ops::Deref;
struct Foo;
impl Deref<Foo> for Foo {
impl Deref for Foo {
type Target = Foo;
fn deref(&self) -> &Foo {
self
}

View File

@ -8,8 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(associated_types)]
struct MyPtr<'a>(&'a mut uint);
impl<'a> Deref<uint> for MyPtr<'a> {
impl<'a> Deref for MyPtr<'a> {
type Target = uint;
fn deref<'b>(&'b self) -> &'b uint { self.0 }
}

View File

@ -10,7 +10,7 @@
// Test that `&mut T` implements `DerefMut<T>`
fn inc<T:DerefMut<int>>(mut t: T) {
fn inc<T:Deref<Target=int> + DerefMut>(mut t: T) {
*t += 1;
}

View File

@ -10,7 +10,7 @@
// Test that `&T` and `&mut T` implement `Deref<T>`
fn deref<U:Copy,T:Deref<U>>(t: T) -> U {
fn deref<U:Copy,T:Deref<Target=U>>(t: T) -> U {
*t
}

View File

@ -10,17 +10,21 @@
// Test that a custom deref with a fat pointer return type does not ICE
#![feature(associated_types)]
pub struct Arr {
ptr: Box<[uint]>
}
impl Deref<[uint]> for Arr {
impl Deref for Arr {
type Target = [uint];
fn deref(&self) -> &[uint] {
panic!();
}
}
impl DerefMut<[uint]> for Arr {
impl DerefMut for Arr {
fn deref_mut(&mut self) -> &mut [uint] {
&mut *self.ptr
}

View File

@ -10,11 +10,15 @@
// Test that a custom deref with a fat pointer return type does not ICE
#![feature(associated_types)]
pub struct Arr {
ptr: Box<[uint]>
}
impl Deref<[uint]> for Arr {
impl Deref for Arr {
type Target = [uint];
fn deref(&self) -> &[uint] {
&*self.ptr
}

View File

@ -8,18 +8,22 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(associated_types)]
// Generic unique/owned smaht pointer.
struct Own<T> {
value: *mut T
}
impl<T> Deref<T> for Own<T> {
impl<T> Deref for Own<T> {
type Target = T;
fn deref<'a>(&'a self) -> &'a T {
unsafe { &*self.value }
}
}
impl<T> DerefMut<T> for Own<T> {
impl<T> DerefMut for Own<T> {
fn deref_mut<'a>(&'a mut self) -> &'a mut T {
unsafe { &mut *self.value }
}

View File

@ -8,11 +8,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(associated_types)]
struct Root {
jsref: JSRef
}
impl Deref<JSRef> for Root {
impl Deref for Root {
type Target = JSRef;
fn deref<'a>(&'a self) -> &'a JSRef {
&self.jsref
}
@ -23,7 +27,9 @@ struct JSRef {
node: *const Node
}
impl Deref<Node> for JSRef {
impl Deref for JSRef {
type Target = Node;
fn deref<'a>(&'a self) -> &'a Node {
self.get()
}

View File

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(unboxed_closures)]
#![feature(associated_types, unboxed_closures)]
struct X(Box<int>);
@ -23,14 +23,16 @@ impl Drop for X {
}
}
impl Deref<int> for X {
impl Deref for X {
type Target = int;
fn deref(&self) -> &int {
let &X(box ref x) = self;
x
}
}
impl DerefMut<int> for X {
impl DerefMut for X {
fn deref_mut(&mut self) -> &mut int {
let &X(box ref mut x) = self;
x

View File

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(associated_types)]
use std::cell::Cell;
use std::ops::{Deref, DerefMut};
@ -32,14 +34,16 @@ impl<T> DerefCounter<T> {
}
}
impl<T> Deref<T> for DerefCounter<T> {
impl<T> Deref for DerefCounter<T> {
type Target = T;
fn deref(&self) -> &T {
self.count_imm.set(self.count_imm.get() + 1);
&self.value
}
}
impl<T> DerefMut<T> for DerefCounter<T> {
impl<T> DerefMut for DerefCounter<T> {
fn deref_mut(&mut self) -> &mut T {
self.count_mut += 1;
&mut self.value

View File

@ -8,11 +8,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(associated_types)]
struct DerefArray<'a, T:'a> {
inner: &'a [T]
}
impl<'a, T> Deref<&'a [T]> for DerefArray<'a, T> {
impl<'a, T> Deref for DerefArray<'a, T> {
type Target = &'a [T];
fn deref<'b>(&'b self) -> &'b &'a [T] {
&self.inner
}

View File

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(associated_types)]
use std::rc::Rc;
struct DerefWrapper<X, Y> {
@ -23,7 +25,9 @@ impl<X, Y> DerefWrapper<X, Y> {
}
}
impl<X, Y> Deref<Y> for DerefWrapper<X, Y> {
impl<X, Y> Deref for DerefWrapper<X, Y> {
type Target = Y;
fn deref(&self) -> &Y {
&self.y
}
@ -46,7 +50,9 @@ mod priv_test {
}
}
impl<X, Y> Deref<Y> for DerefWrapperHideX<X, Y> {
impl<X, Y> Deref for DerefWrapperHideX<X, Y> {
type Target = Y;
fn deref(&self) -> &Y {
&self.y
}

View File

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(associated_types)]
use std::ops::Deref;
struct DerefWithHelper<H, T> {
@ -24,7 +26,9 @@ impl<T> Helper<T> for Option<T> {
}
}
impl<T, H: Helper<T>> Deref<T> for DerefWithHelper<H, T> {
impl<T, H: Helper<T>> Deref for DerefWithHelper<H, T> {
type Target = T;
fn deref(&self) -> &T {
self.helper.helper_borrow()
}

View File

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(associated_types)]
use std::cell::Cell;
use std::ops::{Deref, DerefMut};
use std::vec::Vec;
@ -32,14 +34,16 @@ impl<T> DerefCounter<T> {
}
}
impl<T> Deref<T> for DerefCounter<T> {
impl<T> Deref for DerefCounter<T> {
type Target = T;
fn deref(&self) -> &T {
self.count_imm.set(self.count_imm.get() + 1);
&self.value
}
}
impl<T> DerefMut<T> for DerefCounter<T> {
impl<T> DerefMut for DerefCounter<T> {
fn deref_mut(&mut self) -> &mut T {
self.count_mut += 1;
&mut self.value