Skip to content

Commit 64b7c22

Browse files
author
Jorge Aparicio
committed
core: use assoc types in Deref[Mut]
1 parent d555772 commit 64b7c22

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+256
-107
lines changed

src/liballoc/arc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ impl<T> BorrowFrom<Arc<T>> for T {
247247
}
248248

249249
#[experimental = "Deref is experimental."]
250-
impl<T> Deref<T> for Arc<T> {
250+
impl<T> Deref for Arc<T> {
251+
type Target = T;
252+
251253
#[inline]
252254
fn deref(&self) -> &T {
253255
&self.inner().data

src/liballoc/boxed.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@ impl fmt::Show for Box<Any+'static> {
153153
}
154154
}
155155

156-
impl<Sized? T> Deref<T> for Box<T> {
156+
impl<Sized? T> Deref for Box<T> {
157+
type Target = T;
158+
157159
fn deref(&self) -> &T { &**self }
158160
}
159161

160-
impl<Sized? T> DerefMut<T> for Box<T> {
162+
impl<Sized? T> DerefMut for Box<T> {
161163
fn deref_mut(&mut self) -> &mut T { &mut **self }
162164
}
163165

@@ -210,7 +212,7 @@ mod test {
210212

211213
#[test]
212214
fn deref() {
213-
fn homura<T: Deref<i32>>(_: T) { }
215+
fn homura<T: Deref<Target=i32>>(_: T) { }
214216
homura(box 765i32);
215217
}
216218
}

src/liballoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
#![no_std]
6767
#![feature(lang_items, phase, unsafe_destructor, default_type_params)]
68+
#![feature(associated_types)]
6869

6970
#[phase(plugin, link)]
7071
extern crate core;

src/liballoc/rc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ impl<T> BorrowFrom<Rc<T>> for T {
355355
}
356356

357357
#[experimental = "Deref is experimental."]
358-
impl<T> Deref<T> for Rc<T> {
358+
impl<T> Deref for Rc<T> {
359+
type Target = T;
360+
359361
#[inline(always)]
360362
fn deref(&self) -> &T {
361363
&self.inner().value

src/libcollections/btree/map.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,13 +515,15 @@ mod stack {
515515
marker: marker::InvariantLifetime<'id>
516516
}
517517

518-
impl<'id, T> Deref<T> for IdRef<'id, T> {
518+
impl<'id, T> Deref for IdRef<'id, T> {
519+
type Target = T;
520+
519521
fn deref(&self) -> &T {
520522
&*self.inner
521523
}
522524
}
523525

524-
impl<'id, T> DerefMut<T> for IdRef<'id, T> {
526+
impl<'id, T> DerefMut for IdRef<'id, T> {
525527
fn deref_mut(&mut self) -> &mut T {
526528
&mut *self.inner
527529
}

src/libcollections/btree/node.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,9 @@ impl<K: Clone, V: Clone> Clone for Node<K, V> {
455455
/// flag: &'a Cell<bool>,
456456
/// }
457457
///
458-
/// impl<'a> Deref<Node<uint, uint>> for Nasty<'a> {
458+
/// impl<'a> Deref for Nasty<'a> {
459+
/// type Target = Node<uint, uint>;
460+
///
459461
/// fn deref(&self) -> &Node<uint, uint> {
460462
/// if self.flag.get() {
461463
/// &*self.second
@@ -511,7 +513,7 @@ impl<K: Ord, V> Node<K, V> {
511513
/// Searches for the given key in the node. If it finds an exact match,
512514
/// `Found` will be yielded with the matching index. If it doesn't find an exact match,
513515
/// `GoDown` will be yielded with the index of the subtree the key must lie in.
514-
pub fn search<Sized? Q, NodeRef: Deref<Node<K, V>>>(node: NodeRef, key: &Q)
516+
pub fn search<Sized? Q, NodeRef: Deref<Target=Node<K, V>>>(node: NodeRef, key: &Q)
515517
-> SearchResult<NodeRef> where Q: BorrowFrom<K> + Ord {
516518
// FIXME(Gankro): Tune when to search linear or binary based on B (and maybe K/V).
517519
// For the B configured as of this writing (B = 6), binary search was *significantly*
@@ -588,7 +590,7 @@ impl <K, V> Node<K, V> {
588590
}
589591
}
590592

591-
impl<K, V, NodeRef: Deref<Node<K, V>>, Type, NodeType> Handle<NodeRef, Type, NodeType> {
593+
impl<K, V, NodeRef: Deref<Target=Node<K, V>>, Type, NodeType> Handle<NodeRef, Type, NodeType> {
592594
/// Returns a reference to the node that contains the pointed-to edge or key/value pair. This
593595
/// is very different from `edge` and `edge_mut` because those return children of the node
594596
/// returned by `node`.
@@ -597,7 +599,9 @@ impl<K, V, NodeRef: Deref<Node<K, V>>, Type, NodeType> Handle<NodeRef, Type, Nod
597599
}
598600
}
599601

600-
impl<K, V, NodeRef: DerefMut<Node<K, V>>, Type, NodeType> Handle<NodeRef, Type, NodeType> {
602+
impl<K, V, NodeRef, Type, NodeType> Handle<NodeRef, Type, NodeType> where
603+
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
604+
{
601605
/// Converts a handle into one that stores the same information using a raw pointer. This can
602606
/// be useful in conjunction with `from_raw` when the type system is insufficient for
603607
/// 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
653657
}
654658
}
655659

656-
impl<K, V, NodeRef: Deref<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Internal> {
660+
impl<K, V, NodeRef: Deref<Target=Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Internal> {
657661
// This doesn't exist because there are no uses for it,
658662
// but is fine to add, analagous to edge_mut.
659663
//
@@ -667,7 +671,7 @@ pub enum ForceResult<NodeRef, Type> {
667671
Internal(Handle<NodeRef, Type, handle::Internal>)
668672
}
669673

670-
impl<K, V, NodeRef: Deref<Node<K, V>>, Type> Handle<NodeRef, Type, handle::LeafOrInternal> {
674+
impl<K, V, NodeRef: Deref<Target=Node<K, V>>, Type> Handle<NodeRef, Type, handle::LeafOrInternal> {
671675
/// Figure out whether this handle is pointing to something in a leaf node or to something in
672676
/// an internal node, clarifying the type according to the result.
673677
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
684688
}
685689
}
686690
}
687-
688-
impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Leaf> {
691+
impl<K, V, NodeRef> Handle<NodeRef, handle::Edge, handle::Leaf> where
692+
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
693+
{
689694
/// Tries to insert this key-value pair at the given index in this leaf node
690695
/// If the node is full, we have to split it.
691696
///
@@ -717,7 +722,9 @@ impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::
717722
}
718723
}
719724

720-
impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::Edge, handle::Internal> {
725+
impl<K, V, NodeRef> Handle<NodeRef, handle::Edge, handle::Internal> where
726+
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
727+
{
721728
/// Returns a mutable reference to the edge pointed-to by this handle. This should not be
722729
/// confused with `node`, which references the parent node of what is returned here.
723730
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::
800807
}
801808
}
802809

803-
impl<K, V, NodeRef: DerefMut<Node<K, V>>, NodeType> Handle<NodeRef, handle::Edge, NodeType> {
810+
impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::Edge, NodeType> where
811+
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
812+
{
804813
/// Gets the handle pointing to the key/value pair just to the left of the pointed-to edge.
805814
/// This is unsafe because the handle might point to the first edge in the node, which has no
806815
/// pair to its left.
@@ -862,7 +871,7 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<&'a mut Node<K, V>, handle::KV, NodeType
862871
}
863872
}
864873

865-
impl<'a, K: 'a, V: 'a, NodeRef: Deref<Node<K, V>> + 'a, NodeType> Handle<NodeRef, handle::KV,
874+
impl<'a, K: 'a, V: 'a, NodeRef: Deref<Target=Node<K, V>> + 'a, NodeType> Handle<NodeRef, handle::KV,
866875
NodeType> {
867876
// These are fine to include, but are currently unneeded.
868877
//
@@ -881,8 +890,9 @@ impl<'a, K: 'a, V: 'a, NodeRef: Deref<Node<K, V>> + 'a, NodeType> Handle<NodeRef
881890
// }
882891
}
883892

884-
impl<'a, K: 'a, V: 'a, NodeRef: DerefMut<Node<K, V>> + 'a, NodeType> Handle<NodeRef, handle::KV,
885-
NodeType> {
893+
impl<'a, K: 'a, V: 'a, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
894+
NodeRef: 'a + Deref<Target=Node<K, V>> + DerefMut,
895+
{
886896
/// Returns a mutable reference to the key pointed-to by this handle. This doesn't return a
887897
/// reference with a lifetime as large as `into_kv_mut`, but it also does not consume the
888898
/// handle.
@@ -898,7 +908,9 @@ impl<'a, K: 'a, V: 'a, NodeRef: DerefMut<Node<K, V>> + 'a, NodeType> Handle<Node
898908
}
899909
}
900910

901-
impl<K, V, NodeRef: DerefMut<Node<K, V>>, NodeType> Handle<NodeRef, handle::KV, NodeType> {
911+
impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
912+
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
913+
{
902914
/// Gets the handle pointing to the edge immediately to the left of the key/value pair pointed
903915
/// to by this handle.
904916
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,
918930
}
919931
}
920932

921-
impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::KV, handle::Leaf> {
933+
impl<K, V, NodeRef> Handle<NodeRef, handle::KV, handle::Leaf> where
934+
NodeRef: Deref<Target=Node<K, V>> + DerefMut,
935+
{
922936
/// Removes the key/value pair at the handle's location.
923937
///
924938
/// # Panics (in debug build)
@@ -929,7 +943,9 @@ impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::KV, handle::Le
929943
}
930944
}
931945

932-
impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::KV, handle::Internal> {
946+
impl<K, V, NodeRef> Handle<NodeRef, handle::KV, handle::Internal> where
947+
NodeRef: Deref<Target=Node<K, V>> + DerefMut
948+
{
933949
/// Steal! Stealing is roughly analogous to a binary tree rotation.
934950
/// In this case, we're "rotating" right.
935951
unsafe fn steal_rightward(&mut self) {

src/libcollections/string.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,9 @@ impl ops::Slice<uint, str> for String {
935935
}
936936

937937
#[experimental = "waiting on Deref stabilization"]
938-
impl ops::Deref<str> for String {
938+
impl ops::Deref for String {
939+
type Target = str;
940+
939941
fn deref<'a>(&'a self) -> &'a str {
940942
unsafe { mem::transmute(self.vec[]) }
941943
}
@@ -947,7 +949,9 @@ pub struct DerefString<'a> {
947949
x: DerefVec<'a, u8>
948950
}
949951

950-
impl<'a> Deref<String> for DerefString<'a> {
952+
impl<'a> Deref for DerefString<'a> {
953+
type Target = String;
954+
951955
fn deref<'b>(&'b self) -> &'b String {
952956
unsafe { mem::transmute(&*self.x) }
953957
}

src/libcollections/vec.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,12 +1301,14 @@ impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
13011301
}
13021302

13031303
#[experimental = "waiting on Deref stability"]
1304-
impl<T> ops::Deref<[T]> for Vec<T> {
1304+
impl<T> ops::Deref for Vec<T> {
1305+
type Target = [T];
1306+
13051307
fn deref<'a>(&'a self) -> &'a [T] { self.as_slice() }
13061308
}
13071309

13081310
#[experimental = "waiting on DerefMut stability"]
1309-
impl<T> ops::DerefMut<[T]> for Vec<T> {
1311+
impl<T> ops::DerefMut for Vec<T> {
13101312
fn deref_mut<'a>(&'a mut self) -> &'a mut [T] { self.as_mut_slice() }
13111313
}
13121314

@@ -1716,7 +1718,9 @@ pub struct DerefVec<'a, T> {
17161718
}
17171719

17181720
#[experimental]
1719-
impl<'a, T> Deref<Vec<T>> for DerefVec<'a, T> {
1721+
impl<'a, T> Deref for DerefVec<'a, T> {
1722+
type Target = Vec<T>;
1723+
17201724
fn deref<'b>(&'b self) -> &'b Vec<T> {
17211725
&self.x
17221726
}

src/libcore/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ macro_rules! array_impls {
5454
#[stable]
5555
impl<'a, A, B, Rhs> PartialEq<Rhs> for [A; $N] where
5656
A: PartialEq<B>,
57-
Rhs: Deref<[B]>,
57+
Rhs: Deref<Target=[B]>,
5858
{
5959
#[inline(always)]
6060
fn eq(&self, other: &Rhs) -> bool { PartialEq::eq(self[], &**other) }
@@ -65,7 +65,7 @@ macro_rules! array_impls {
6565
#[stable]
6666
impl<'a, A, B, Lhs> PartialEq<[B; $N]> for Lhs where
6767
A: PartialEq<B>,
68-
Lhs: Deref<[A]>
68+
Lhs: Deref<Target=[A]>
6969
{
7070
#[inline(always)]
7171
fn eq(&self, other: &[B; $N]) -> bool { PartialEq::eq(&**self, other[]) }

src/libcore/borrow.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ impl<'a, T, Sized? B> Cow<'a, T, B> where B: ToOwned<T> {
191191
}
192192
}
193193

194-
impl<'a, T, Sized? B> Deref<B> for Cow<'a, T, B> where B: ToOwned<T> {
194+
impl<'a, T, Sized? B> Deref for Cow<'a, T, B> where B: ToOwned<T> {
195+
type Target = B;
196+
195197
fn deref(&self) -> &B {
196198
match *self {
197199
Borrowed(borrowed) => borrowed,

src/libcore/cell.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ pub struct Ref<'b, T:'b> {
422422
}
423423

424424
#[unstable = "waiting for `Deref` to become stable"]
425-
impl<'b, T> Deref<T> for Ref<'b, T> {
425+
impl<'b, T> Deref for Ref<'b, T> {
426+
type Target = T;
427+
426428
#[inline]
427429
fn deref<'a>(&'a self) -> &'a T {
428430
self._value
@@ -478,15 +480,17 @@ pub struct RefMut<'b, T:'b> {
478480
}
479481

480482
#[unstable = "waiting for `Deref` to become stable"]
481-
impl<'b, T> Deref<T> for RefMut<'b, T> {
483+
impl<'b, T> Deref for RefMut<'b, T> {
484+
type Target = T;
485+
482486
#[inline]
483487
fn deref<'a>(&'a self) -> &'a T {
484488
self._value
485489
}
486490
}
487491

488492
#[unstable = "waiting for `DerefMut` to become stable"]
489-
impl<'b, T> DerefMut<T> for RefMut<'b, T> {
493+
impl<'b, T> DerefMut for RefMut<'b, T> {
490494
#[inline]
491495
fn deref_mut<'a>(&'a mut self) -> &'a mut T {
492496
self._value

src/libcore/iter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ pub trait IteratorCloneExt<A> {
11741174
}
11751175

11761176
#[unstable = "trait is unstable"]
1177-
impl<A: Clone, D: Deref<A>, I: Iterator<D>> IteratorCloneExt<A> for I {
1177+
impl<A: Clone, D: Deref<Target=A>, I: Iterator<D>> IteratorCloneExt<A> for I {
11781178
fn cloned(self) -> Cloned<I> {
11791179
Cloned { it: self }
11801180
}
@@ -1185,7 +1185,7 @@ pub struct Cloned<I> {
11851185
it: I,
11861186
}
11871187

1188-
impl<A: Clone, D: Deref<A>, I: Iterator<D>> Iterator<A> for Cloned<I> {
1188+
impl<A: Clone, D: Deref<Target=A>, I: Iterator<D>> Iterator<A> for Cloned<I> {
11891189
fn next(&mut self) -> Option<A> {
11901190
self.it.next().cloned()
11911191
}
@@ -1195,15 +1195,15 @@ impl<A: Clone, D: Deref<A>, I: Iterator<D>> Iterator<A> for Cloned<I> {
11951195
}
11961196
}
11971197

1198-
impl<A: Clone, D: Deref<A>, I: DoubleEndedIterator<D>>
1198+
impl<A: Clone, D: Deref<Target=A>, I: DoubleEndedIterator<D>>
11991199
DoubleEndedIterator<A> for Cloned<I> {
12001200
fn next_back(&mut self) -> Option<A> {
12011201
self.it.next_back().cloned()
12021202
}
12031203
}
12041204

12051205
#[unstable = "trait is unstable"]
1206-
impl<A: Clone, D: Deref<A>, I: ExactSizeIterator<D>> ExactSizeIterator<A> for Cloned<I> {}
1206+
impl<A: Clone, D: Deref<Target=A>, I: ExactSizeIterator<D>> ExactSizeIterator<A> for Cloned<I> {}
12071207

12081208
#[unstable = "recently renamed for extension trait conventions"]
12091209
/// An extension trait for cloneable iterators.

src/libcore/nonzero.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ impl<T: Zeroable> NonZero<T> {
4444
}
4545
}
4646

47-
impl<T: Zeroable> Deref<T> for NonZero<T> {
47+
impl<T: Zeroable> Deref for NonZero<T> {
48+
type Target = T;
49+
4850
#[inline]
4951
fn deref<'a>(&'a self) -> &'a T {
5052
let NonZero(ref inner) = *self;

0 commit comments

Comments
 (0)