Skip to content

Commit 6693b4d

Browse files
author
Clar Charr
committed
Move Index to module.
1 parent fefa521 commit 6693b4d

File tree

2 files changed

+162
-149
lines changed

2 files changed

+162
-149
lines changed

src/libcore/ops/index.rs

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/// The `Index` trait is used to specify the functionality of indexing operations
12+
/// like `container[index]` when used in an immutable context.
13+
///
14+
/// `container[index]` is actually syntactic sugar for `*container.index(index)`,
15+
/// but only when used as an immutable value. If a mutable value is requested,
16+
/// [`IndexMut`] is used instead. This allows nice things such as
17+
/// `let value = v[index]` if `value` implements [`Copy`].
18+
///
19+
/// [`IndexMut`]: ../../std/ops/trait.IndexMut.html
20+
/// [`Copy`]: ../../std/marker/trait.Copy.html
21+
///
22+
/// # Examples
23+
///
24+
/// The following example implements `Index` on a read-only `NucleotideCount`
25+
/// container, enabling individual counts to be retrieved with index syntax.
26+
///
27+
/// ```
28+
/// use std::ops::Index;
29+
///
30+
/// enum Nucleotide {
31+
/// A,
32+
/// C,
33+
/// G,
34+
/// T,
35+
/// }
36+
///
37+
/// struct NucleotideCount {
38+
/// a: usize,
39+
/// c: usize,
40+
/// g: usize,
41+
/// t: usize,
42+
/// }
43+
///
44+
/// impl Index<Nucleotide> for NucleotideCount {
45+
/// type Output = usize;
46+
///
47+
/// fn index(&self, nucleotide: Nucleotide) -> &usize {
48+
/// match nucleotide {
49+
/// Nucleotide::A => &self.a,
50+
/// Nucleotide::C => &self.c,
51+
/// Nucleotide::G => &self.g,
52+
/// Nucleotide::T => &self.t,
53+
/// }
54+
/// }
55+
/// }
56+
///
57+
/// let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
58+
/// assert_eq!(nucleotide_count[Nucleotide::A], 14);
59+
/// assert_eq!(nucleotide_count[Nucleotide::C], 9);
60+
/// assert_eq!(nucleotide_count[Nucleotide::G], 10);
61+
/// assert_eq!(nucleotide_count[Nucleotide::T], 12);
62+
/// ```
63+
#[lang = "index"]
64+
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
65+
#[stable(feature = "rust1", since = "1.0.0")]
66+
pub trait Index<Idx: ?Sized> {
67+
/// The returned type after indexing
68+
#[stable(feature = "rust1", since = "1.0.0")]
69+
type Output: ?Sized;
70+
71+
/// The method for the indexing (`container[index]`) operation
72+
#[stable(feature = "rust1", since = "1.0.0")]
73+
fn index(&self, index: Idx) -> &Self::Output;
74+
}
75+
76+
/// The `IndexMut` trait is used to specify the functionality of indexing
77+
/// operations like `container[index]` when used in a mutable context.
78+
///
79+
/// `container[index]` is actually syntactic sugar for
80+
/// `*container.index_mut(index)`, but only when used as a mutable value. If
81+
/// an immutable value is requested, the [`Index`] trait is used instead. This
82+
/// allows nice things such as `v[index] = value` if `value` implements [`Copy`].
83+
///
84+
/// [`Index`]: ../../std/ops/trait.Index.html
85+
/// [`Copy`]: ../../std/marker/trait.Copy.html
86+
///
87+
/// # Examples
88+
///
89+
/// A very simple implementation of a `Balance` struct that has two sides, where
90+
/// each can be indexed mutably and immutably.
91+
///
92+
/// ```
93+
/// use std::ops::{Index,IndexMut};
94+
///
95+
/// #[derive(Debug)]
96+
/// enum Side {
97+
/// Left,
98+
/// Right,
99+
/// }
100+
///
101+
/// #[derive(Debug, PartialEq)]
102+
/// enum Weight {
103+
/// Kilogram(f32),
104+
/// Pound(f32),
105+
/// }
106+
///
107+
/// struct Balance {
108+
/// pub left: Weight,
109+
/// pub right:Weight,
110+
/// }
111+
///
112+
/// impl Index<Side> for Balance {
113+
/// type Output = Weight;
114+
///
115+
/// fn index<'a>(&'a self, index: Side) -> &'a Weight {
116+
/// println!("Accessing {:?}-side of balance immutably", index);
117+
/// match index {
118+
/// Side::Left => &self.left,
119+
/// Side::Right => &self.right,
120+
/// }
121+
/// }
122+
/// }
123+
///
124+
/// impl IndexMut<Side> for Balance {
125+
/// fn index_mut<'a>(&'a mut self, index: Side) -> &'a mut Weight {
126+
/// println!("Accessing {:?}-side of balance mutably", index);
127+
/// match index {
128+
/// Side::Left => &mut self.left,
129+
/// Side::Right => &mut self.right,
130+
/// }
131+
/// }
132+
/// }
133+
///
134+
/// fn main() {
135+
/// let mut balance = Balance {
136+
/// right: Weight::Kilogram(2.5),
137+
/// left: Weight::Pound(1.5),
138+
/// };
139+
///
140+
/// // In this case balance[Side::Right] is sugar for
141+
/// // *balance.index(Side::Right), since we are only reading
142+
/// // balance[Side::Right], not writing it.
143+
/// assert_eq!(balance[Side::Right],Weight::Kilogram(2.5));
144+
///
145+
/// // However in this case balance[Side::Left] is sugar for
146+
/// // *balance.index_mut(Side::Left), since we are writing
147+
/// // balance[Side::Left].
148+
/// balance[Side::Left] = Weight::Kilogram(3.0);
149+
/// }
150+
/// ```
151+
#[lang = "index_mut"]
152+
#[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
153+
#[stable(feature = "rust1", since = "1.0.0")]
154+
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
155+
/// The method for the mutable indexing (`container[index]`) operation
156+
#[stable(feature = "rust1", since = "1.0.0")]
157+
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
158+
}

src/libcore/ops/mod.rs

Lines changed: 4 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ mod arith;
151151
mod bit;
152152
mod deref;
153153
mod function;
154+
mod index;
154155
mod place;
155156
mod range;
156157
mod try;
@@ -171,6 +172,9 @@ pub use self::deref::{Deref, DerefMut};
171172
#[stable(feature = "rust1", since = "1.0.0")]
172173
pub use self::function::{Fn, FnMut, FnOnce};
173174

175+
#[stable(feature = "rust1", since = "1.0.0")]
176+
pub use self::index::{Index, IndexMut};
177+
174178
#[stable(feature = "rust1", since = "1.0.0")]
175179
pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
176180

@@ -278,155 +282,6 @@ pub trait Drop {
278282
fn drop(&mut self);
279283
}
280284

281-
/// The `Index` trait is used to specify the functionality of indexing operations
282-
/// like `container[index]` when used in an immutable context.
283-
///
284-
/// `container[index]` is actually syntactic sugar for `*container.index(index)`,
285-
/// but only when used as an immutable value. If a mutable value is requested,
286-
/// [`IndexMut`] is used instead. This allows nice things such as
287-
/// `let value = v[index]` if `value` implements [`Copy`].
288-
///
289-
/// [`IndexMut`]: ../../std/ops/trait.IndexMut.html
290-
/// [`Copy`]: ../../std/marker/trait.Copy.html
291-
///
292-
/// # Examples
293-
///
294-
/// The following example implements `Index` on a read-only `NucleotideCount`
295-
/// container, enabling individual counts to be retrieved with index syntax.
296-
///
297-
/// ```
298-
/// use std::ops::Index;
299-
///
300-
/// enum Nucleotide {
301-
/// A,
302-
/// C,
303-
/// G,
304-
/// T,
305-
/// }
306-
///
307-
/// struct NucleotideCount {
308-
/// a: usize,
309-
/// c: usize,
310-
/// g: usize,
311-
/// t: usize,
312-
/// }
313-
///
314-
/// impl Index<Nucleotide> for NucleotideCount {
315-
/// type Output = usize;
316-
///
317-
/// fn index(&self, nucleotide: Nucleotide) -> &usize {
318-
/// match nucleotide {
319-
/// Nucleotide::A => &self.a,
320-
/// Nucleotide::C => &self.c,
321-
/// Nucleotide::G => &self.g,
322-
/// Nucleotide::T => &self.t,
323-
/// }
324-
/// }
325-
/// }
326-
///
327-
/// let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
328-
/// assert_eq!(nucleotide_count[Nucleotide::A], 14);
329-
/// assert_eq!(nucleotide_count[Nucleotide::C], 9);
330-
/// assert_eq!(nucleotide_count[Nucleotide::G], 10);
331-
/// assert_eq!(nucleotide_count[Nucleotide::T], 12);
332-
/// ```
333-
#[lang = "index"]
334-
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
335-
#[stable(feature = "rust1", since = "1.0.0")]
336-
pub trait Index<Idx: ?Sized> {
337-
/// The returned type after indexing
338-
#[stable(feature = "rust1", since = "1.0.0")]
339-
type Output: ?Sized;
340-
341-
/// The method for the indexing (`container[index]`) operation
342-
#[stable(feature = "rust1", since = "1.0.0")]
343-
fn index(&self, index: Idx) -> &Self::Output;
344-
}
345-
346-
/// The `IndexMut` trait is used to specify the functionality of indexing
347-
/// operations like `container[index]` when used in a mutable context.
348-
///
349-
/// `container[index]` is actually syntactic sugar for
350-
/// `*container.index_mut(index)`, but only when used as a mutable value. If
351-
/// an immutable value is requested, the [`Index`] trait is used instead. This
352-
/// allows nice things such as `v[index] = value` if `value` implements [`Copy`].
353-
///
354-
/// [`Index`]: ../../std/ops/trait.Index.html
355-
/// [`Copy`]: ../../std/marker/trait.Copy.html
356-
///
357-
/// # Examples
358-
///
359-
/// A very simple implementation of a `Balance` struct that has two sides, where
360-
/// each can be indexed mutably and immutably.
361-
///
362-
/// ```
363-
/// use std::ops::{Index,IndexMut};
364-
///
365-
/// #[derive(Debug)]
366-
/// enum Side {
367-
/// Left,
368-
/// Right,
369-
/// }
370-
///
371-
/// #[derive(Debug, PartialEq)]
372-
/// enum Weight {
373-
/// Kilogram(f32),
374-
/// Pound(f32),
375-
/// }
376-
///
377-
/// struct Balance {
378-
/// pub left: Weight,
379-
/// pub right:Weight,
380-
/// }
381-
///
382-
/// impl Index<Side> for Balance {
383-
/// type Output = Weight;
384-
///
385-
/// fn index<'a>(&'a self, index: Side) -> &'a Weight {
386-
/// println!("Accessing {:?}-side of balance immutably", index);
387-
/// match index {
388-
/// Side::Left => &self.left,
389-
/// Side::Right => &self.right,
390-
/// }
391-
/// }
392-
/// }
393-
///
394-
/// impl IndexMut<Side> for Balance {
395-
/// fn index_mut<'a>(&'a mut self, index: Side) -> &'a mut Weight {
396-
/// println!("Accessing {:?}-side of balance mutably", index);
397-
/// match index {
398-
/// Side::Left => &mut self.left,
399-
/// Side::Right => &mut self.right,
400-
/// }
401-
/// }
402-
/// }
403-
///
404-
/// fn main() {
405-
/// let mut balance = Balance {
406-
/// right: Weight::Kilogram(2.5),
407-
/// left: Weight::Pound(1.5),
408-
/// };
409-
///
410-
/// // In this case balance[Side::Right] is sugar for
411-
/// // *balance.index(Side::Right), since we are only reading
412-
/// // balance[Side::Right], not writing it.
413-
/// assert_eq!(balance[Side::Right],Weight::Kilogram(2.5));
414-
///
415-
/// // However in this case balance[Side::Left] is sugar for
416-
/// // *balance.index_mut(Side::Left), since we are writing
417-
/// // balance[Side::Left].
418-
/// balance[Side::Left] = Weight::Kilogram(3.0);
419-
/// }
420-
/// ```
421-
#[lang = "index_mut"]
422-
#[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
423-
#[stable(feature = "rust1", since = "1.0.0")]
424-
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
425-
/// The method for the mutable indexing (`container[index]`) operation
426-
#[stable(feature = "rust1", since = "1.0.0")]
427-
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
428-
}
429-
430285
/// Trait that indicates that this is a pointer or a wrapper for one,
431286
/// where unsizing can be performed on the pointee.
432287
///

0 commit comments

Comments
 (0)