Skip to content

Commit 7e146eb

Browse files
committed
implements index on collection
1 parent cec259d commit 7e146eb

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/id.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,22 @@ impl<T> IntoIterator for Collection<T> {
145145
self.0.into_iter()
146146
}
147147
}
148+
149+
impl<Q, T> std::ops::Index<&Q> for Collection<T>
150+
where
151+
Id<T>: std::borrow::Borrow<Q>,
152+
Q: Eq + std::hash::Hash,
153+
{
154+
type Output = T;
155+
156+
/// Returns a reference to the value corresponding to the supplied key.
157+
///
158+
/// # Panics
159+
///
160+
/// Panics if the key is not present in the [Collection].
161+
/// This can happens only if the key was removed from the collection at one point.
162+
/// If no key are removed from the [Collection], you can safely use this method.
163+
fn index(&self, k: &Q) -> &Self::Output {
164+
&self.0[k]
165+
}
166+
}

0 commit comments

Comments
 (0)