hexga_core/collections/
get.rs

1//! Generalisation over collection, such as `get`, `get_mut`, `get_many_mut`, `swap`, `replace`, `set`...
2//!
3//! Each trait follow this convention when adding a new function `foo`  :
4//!
5//! `fn try_get_foo(...) -> Result<O,E>`
6//!
7//! `fn foo(...) -> Option<O>` (or `bool` instead of `Option<()>` when `try_get_foo` return a `Result<(), E>`)
8//!
9//! `fn foo_or_panic(...) -> O`
10//!
11//! `unsafe fn foo_unchecked(...) -> O`
12
13use std::ops::Range;
14use super::*;
15
16
17/// The collection have a quick way to access each element, where the index is copyable
18pub trait Get<Idx> //where Idx : Borrow<Q> //: Index<Idx>
19{
20    type Output : ?Sized;
21
22    /// Returns a reference to the value.
23    fn get(&self, index : Idx) -> Option<&Self::Output>;
24    /// Returns a reference to the value.
25    #[inline(always)]
26    #[track_caller]
27    fn get_or_panic(&self, index : Idx) -> &Self::Output { self.get(index).expect("invalid index") }
28    /// Returns a reference to the value.
29    #[inline(always)]
30    #[track_caller]
31    unsafe fn get_unchecked(&self, index : Idx) -> &Self::Output { self.get(index).expect("invalid index") }
32
33    /// True if `get(index)` return [Some], false otherwise.
34    #[inline(always)]
35    fn is_index_valid(&self, index : Idx) -> bool { self.get(index).is_some() }
36    /// True if `get(index)` return [None], false otherwise.
37    #[inline(always)]
38    fn is_index_invalid(&self, index : Idx) -> bool { self.get(index).is_none() }
39}
40
41pub trait TryGet<Idx> : Get<Idx>
42{
43    // TODO : Should the Error have a lifetime parameter of self ? Then the caller call error.to_owned() to store it ?
44    type Error;
45    /// Returns a reference to the value.
46    fn try_get(&self, index : Idx) -> Result<&Self::Output, Self::Error>;
47}
48
49
50
51pub trait GetMut<Idx> : Get<Idx>
52{
53    /// Returns a mutable reference to the value.
54    fn get_mut(&mut self, index : Idx) -> Option<&mut Self::Output>;
55    #[inline(always)]
56    #[track_caller]
57    fn get_mut_or_panic(&mut self, idx : Idx) -> &mut Self::Output { self.get_mut(idx).expect("invalid index") }
58    /// Returns a mutable reference to the value.
59    #[inline(always)]
60    #[track_caller]
61    unsafe fn get_unchecked_mut(&mut self, idx : Idx) -> &mut Self::Output { self.get_mut(idx).expect("invalid index") }
62
63
64    /// Replace the value and return the old one.
65    ///
66    /// This operation is an [`involution`](https://en.wikipedia.org/wiki/Involution_(mathematics)):
67    /// Replacing a value twice (first with a new value, then with the previously returned value) leaves the collection unchanged.
68    #[inline(always)]
69    fn replace(&mut self, index : Idx, value : Self::Output) -> Option<Self::Output> where Self::Output : Sized { self.get_mut(index).map(|dest| std::mem::replace(dest, value)) }
70    /// Replace the value and return the old one.
71    ///
72    /// This operation is an [`involution`](https://en.wikipedia.org/wiki/Involution_(mathematics)):
73    /// Replacing a value twice (first with a new value, then with the previously returned value) leaves the collection unchanged.
74    #[inline(always)]
75    #[track_caller]
76    fn replace_or_panic(&mut self, index : Idx, value : Self::Output) -> Self::Output where Self::Output : Sized { self.replace(index, value).expect("invalid index") }
77    /// Replace the value and return the old one.
78    ///
79    /// This operation is an [`involution`](https://en.wikipedia.org/wiki/Involution_(mathematics)):
80    /// Replacing a value twice (first with a new value, then with the previously returned value) leaves the collection unchanged.
81    #[inline(always)]
82    #[track_caller]
83    unsafe fn replace_unchecked(&mut self, index : Idx, value : Self::Output) -> Self::Output where Self::Output : Sized { std::mem::replace(unsafe { self.get_unchecked_mut(index) }, value) }
84
85    /// Set the value and drop the previous one.
86    #[inline(always)]
87    fn set(&mut self, index : Idx, value : Self::Output) -> bool where Self::Output : Sized { self.replace(index, value).map(|_| ()).is_some() }
88    /// Set the value and drop the previous one.
89    #[inline(always)]
90    #[track_caller]
91    fn set_or_panic(&mut self, index : Idx, value : Self::Output) -> &mut Self where Self::Output : Sized { assert!(self.set(index, value), "invalid index"); self }
92    #[inline(always)]
93    #[track_caller]
94    unsafe fn set_unchecked(&mut self, index : Idx, value : Self::Output) -> &mut Self where Self::Output : Sized { unsafe { self.replace_unchecked(index, value) }; self }
95}
96
97pub trait TryGetMut<Idx> : TryGet<Idx>
98{
99    // Should TryGetMut have it own error type ?
100
101    /// Returns a mutable reference to the value.
102    fn try_get_mut(&mut self, index : Idx) -> Result<&mut Self::Output, Self::Error>;
103
104    /// Replace the value and return the old one.
105    ///
106    /// This operation is an [`involution`](https://en.wikipedia.org/wiki/Involution_(mathematics)):
107    /// Replacing a value twice (first with a new value, then with the previously returned value) leaves the collection unchanged.
108    #[inline(always)]
109    fn try_replace(&mut self, index : Idx, value : Self::Output) -> Result<Self::Output, Self::Error> where Self::Output : Sized { self.try_get_mut(index).map(|dest| std::mem::replace(dest, value)) }
110
111    /// Set the value and drop the previous one.
112    #[inline(always)]
113    fn try_set(&mut self, index : Idx, value : Self::Output) -> Result<(), Self::Error> where Self::Output : Sized { self.try_replace(index, value).map(|_| ()) }
114}
115
116pub type ManyMutError = std::slice::GetDisjointMutError;
117
118pub trait GetManyMut<Idx> : GetMut<Idx>
119{
120    /// Returns multiples mutables references to the values.
121    /// All values that can be accessed with the indices must be disjoint.
122    #[doc(alias = "get_disjoint_mut")]
123    fn get_many_mut<const N: usize>(&mut self, indices: [Idx; N]) -> Option<[&mut Self::Output;N]> { self.try_get_many_mut(indices).ok() }
124
125    fn try_get_many_mut<const N: usize>(&mut self, indices: [Idx; N]) -> Result<[&mut Self::Output;N], ManyMutError>;
126
127    /// Returns multiples mutables references to the values.
128    /// All values that can be accessed with the indices must be disjoint.
129    #[inline(always)]
130    #[track_caller]
131    #[doc(alias = "get_disjoint_mut_or_panic")]
132    fn get_many_mut_or_panic<const N: usize>(&mut self, indices: [Idx; N]) -> [&mut Self::Output;N] { self.get_many_mut(indices).expect("invalid index") }
133
134
135    /// Returns multiples mutables references to the values.
136    /// All values that can be accessed with the indices must be disjoint.
137    #[inline(always)]
138    #[track_caller]
139    #[doc(alias = "get_disjoint_unchecked_mut")]
140    unsafe fn get_many_unchecked_mut<const N: usize>(&mut self, indices: [Idx; N]) -> [&mut Self::Output;N] { self.get_many_mut(indices).expect("invalid index") }
141
142    /// Swaps the values at two mutable locations, without deinitializing either one.
143    ///
144    /// Swap is symmetric : `foo.try_swap(a, b)` is equivalent to `foo.try_swap(b, a)` and vis versa
145    ///
146    /// Do nothings if some value  overlap or don't exist.
147    #[inline(always)]
148    fn swap(&mut self, a : Idx, b : Idx) -> bool where Self::Output : Sized { self.get_many_mut([a, b]).map(|[a,b]| std::mem::swap(a, b)).is_some() }
149    /// Swaps the values at two mutable locations, without deinitializing either one.
150    ///
151    /// Swap is symmetric : `foo.try_swap(a, b)` is equivalent to `foo.try_swap(b, a)` and vis versa
152    ///
153    /// Panics if any value overlap or don't exist
154    #[inline(always)]
155    #[track_caller]
156    fn swap_or_panic(&mut self, a : Idx, b : Idx) where Self::Output : Sized { assert!(self.swap(a, b), "invalid index") }
157    /// Swaps the values at two mutable locations, without deinitializing either one.
158    ///
159    /// Swap is symmetric : `foo.try_swap(a, b)` is equivalent to `foo.try_swap(b, a)` and vis versa
160    ///
161    /// Do nothings if some value  overlap or don't exist.
162    #[inline(always)]
163    #[track_caller]
164    unsafe fn swap_unchecked(&mut self, a : Idx, b : Idx) where Self::Output : Sized { let [a,b] = unsafe { self.get_many_unchecked_mut([a, b]) }; std::mem::swap(a, b); }
165}
166
167
168
169
170
171
172
173
174
175// TODO: impl it. Split the core crate : hexga_core : hexga_core_syntax, hexga_core_collection
176#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
177#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
178pub struct IndexOutOfRange<Idx=usize, R=std::ops::Range<Idx>>
179{
180    pub index : Idx,
181    pub range : R,
182}
183impl<Idx, R> IndexOutOfRange<Idx, R>
184{
185    pub fn new(index : Idx, range : R) -> Self { Self { index, range }}
186}
187
188impl<Idx,T> TryGet<Idx> for [T] where Idx : SliceIndex<[T]> + Clone
189{
190    type Error = IndexOutOfRange<Idx,Range<usize>>;
191    #[inline(always)]
192    fn try_get(&self, index : Idx) -> Result<&Self::Output, Self::Error> { self.get(index.clone()).ok_or(IndexOutOfRange{ index, range: 0..self.len() }) }
193}
194impl<Idx,T> Get<Idx> for [T] where Idx : SliceIndex<[T]>
195{
196    type Output = <Self as Index<Idx>>::Output;
197    #[inline(always)]
198    fn get(&self, idx : Idx) -> Option<&Self::Output> { self.get(idx) }
199    #[inline(always)]
200    #[track_caller]
201    unsafe fn get_unchecked(&self, idx : Idx) -> &Self::Output { unsafe { self.get_unchecked(idx) } }
202}
203
204impl<Idx,T> TryGetMut<Idx> for [T] where Idx : SliceIndex<[T]> + Clone
205{
206    #[inline(always)]
207    fn try_get_mut(&mut self, idx : Idx) -> Result<&mut Self::Output, Self::Error> { let len = self.len(); self.get_mut(idx.clone()).ok_or(IndexOutOfRange{ index: idx, range: 0..len }) }
208}
209impl<Idx,T> GetMut<Idx> for [T] where Idx : SliceIndex<[T]>
210{
211    #[inline(always)]
212    fn get_mut(&mut self, idx : Idx) -> Option<&mut Self::Output> { self.get_mut(idx) }
213    #[inline(always)]
214    unsafe fn get_unchecked_mut(&mut self, idx : Idx) -> &mut Self::Output { unsafe { self.get_unchecked_mut(idx) } }
215}
216impl<Idx,T> GetManyMut<Idx> for [T] where Idx : SliceIndex<[T]> + GetDisjointMutIndex
217{
218    #[inline(always)]
219    fn get_many_mut<const N: usize>(&mut self, indices: [Idx; N]) -> Option<[&mut Self::Output;N]> { self.get_disjoint_mut(indices).ok() }
220
221    #[inline(always)]
222    fn try_get_many_mut<const N: usize>(&mut self, indices: [Idx; N]) -> Result<[&mut Self::Output;N], ManyMutError> { self.get_disjoint_mut(indices) }
223
224    #[inline(always)]
225    #[track_caller]
226    unsafe fn get_many_unchecked_mut<const N: usize>(&mut self, indices: [Idx; N]) -> [&mut Self::Output;N]  { unsafe { self.get_disjoint_unchecked_mut(indices) } }
227
228
229}
230
231impl<Idx,T,const N : usize> TryGet<Idx> for [T;N] where [T] : TryGet<Idx>
232{
233    type Error = <[T] as TryGet<Idx>>::Error;
234    #[inline(always)]
235    fn try_get(&self, index : Idx) -> Result<&Self::Output, Self::Error> { TryGet::try_get(self.as_slice(), index) }
236}
237
238impl<Idx,T,const N : usize> Get<Idx> for [T;N] where [T] : Get<Idx>
239{
240    type Output =  <[T] as Get<Idx>>::Output;
241    #[inline(always)]
242    fn get(&self, idx : Idx) -> Option<&Self::Output> { Get::get(self.as_slice(), idx) }
243    #[inline(always)]
244    #[track_caller]
245    unsafe fn get_unchecked(&self, idx : Idx) -> &Self::Output { unsafe { Get::get_unchecked(self.as_slice(), idx) } }
246}
247
248impl<Idx,T,const N : usize> TryGetMut<Idx> for [T;N] where [T] : TryGetMut<Idx>
249{
250    #[inline(always)]
251    fn try_get_mut(&mut self, index : Idx) -> Result<&mut Self::Output, Self::Error> { TryGetMut::try_get_mut(self.as_mut_slice(), index) }
252}
253
254impl<Idx,T,const N : usize> GetMut<Idx> for [T;N] where [T] : GetMut<Idx>
255{
256    #[inline(always)]
257    fn get_mut(&mut self, idx : Idx) -> Option<&mut Self::Output> { GetMut::get_mut(self.as_mut_slice(), idx) }
258    #[inline(always)]
259    unsafe fn get_unchecked_mut(&mut self, idx : Idx) -> &mut Self::Output { unsafe { GetMut::get_unchecked_mut(self.as_mut_slice(), idx) } }
260}
261impl<Idx,T,const N : usize> GetManyMut<Idx> for [T;N] where [T] : GetManyMut<Idx>
262{
263    #[inline(always)]
264    fn get_many_mut<const N2: usize>(&mut self, indices: [Idx; N2]) -> Option<[&mut Self::Output;N2]> { GetManyMut::get_many_mut(self.as_mut_slice(), indices) }
265
266    #[inline(always)]
267    fn try_get_many_mut<const N2: usize>(&mut self, indices: [Idx; N2]) -> Result<[&mut Self::Output;N2], ManyMutError> { GetManyMut::try_get_many_mut(self.as_mut_slice(), indices) }
268
269    #[inline(always)]
270    #[track_caller]
271    unsafe fn get_many_unchecked_mut<const N2: usize>(&mut self, indices: [Idx; N2]) -> [&mut Self::Output;N2] { unsafe { GetManyMut::get_many_unchecked_mut(self.as_mut_slice(), indices) } }
272
273
274}
275
276
277impl<Idx,T> TryGet<Idx> for Vec<T> where [T] : TryGet<Idx>
278{
279    type Error = <[T] as TryGet<Idx>>::Error;
280    #[inline(always)]
281    fn try_get(&self, index : Idx) -> Result<&Self::Output, Self::Error> { TryGet::try_get(self.as_slice(), index) }
282}
283impl<Idx,T> Get<Idx> for Vec<T> where [T] : Get<Idx>
284{
285    type Output = <[T] as Get<Idx>>::Output;
286    #[inline(always)]
287    fn get(&self, idx : Idx) ->Option<&Self::Output> { Get::get(self.as_slice(), idx) }
288    #[track_caller]
289    #[inline(always)]
290    unsafe fn get_unchecked(&self, idx : Idx) -> &Self::Output { unsafe { Get::get_unchecked(self.as_slice(), idx) } }
291}
292impl<Idx,T> TryGetMut<Idx> for Vec<T> where [T] : TryGetMut<Idx>
293{
294    #[inline(always)]
295    fn try_get_mut(&mut self, index : Idx) -> Result<&mut Self::Output, Self::Error> { TryGetMut::try_get_mut(self.as_mut_slice(), index) }
296}
297impl<Idx,T> GetMut<Idx> for Vec<T> where [T] : GetMut<Idx>
298{
299    #[inline(always)]
300    fn get_mut(&mut self, idx : Idx) -> Option<&mut Self::Output> { GetMut::get_mut(self.as_mut_slice(), idx) }
301    #[inline(always)]
302    unsafe fn get_unchecked_mut(&mut self, idx : Idx) -> &mut Self::Output { unsafe { GetMut::get_unchecked_mut(self.as_mut_slice(), idx) } }
303}
304impl<Idx,T> GetManyMut<Idx> for Vec<T> where [T] : GetManyMut<Idx>
305{
306    #[track_caller]
307    #[inline(always)]
308    unsafe fn get_many_unchecked_mut<const N: usize>(&mut self, indices: [Idx; N]) -> [&mut Self::Output;N] { unsafe { GetManyMut::get_many_unchecked_mut(self.as_mut_slice(), indices) } }
309
310    #[inline(always)]
311    fn try_get_many_mut<const N: usize>(&mut self, indices: [Idx; N]) -> Result<[&mut Self::Output;N], ManyMutError> { GetManyMut::try_get_many_mut(self.as_mut_slice(), indices) }
312    #[inline(always)]
313    fn get_many_mut<const N: usize>(&mut self, indices: [Idx; N]) -> Option<[&mut Self::Output;N]> { GetManyMut::get_many_mut(self.as_mut_slice(), indices) }
314}
315
316
317impl<T> TryGet<usize> for VecDeque<T>
318{
319    type Error = IndexOutOfRange<usize, std::ops::Range<usize>>;
320    #[inline(always)]
321    fn try_get(&self, index : usize) -> Result<&Self::Output, Self::Error>{ self.get(index).ok_or(IndexOutOfRange{ index, range: 0..self.len() }) }
322}
323impl<T> Get<usize> for VecDeque<T>
324{
325    type Output = <Self as Index<usize>>::Output;
326    #[inline(always)]
327    fn get(&self, idx : usize) -> Option<&Self::Output> { self.get(idx) }
328}
329impl<T> TryGetMut<usize> for VecDeque<T>
330{
331    #[inline(always)]
332    fn try_get_mut(&mut self, index : usize) -> Result<&mut Self::Output, Self::Error>{ let len = self.len(); self.get_mut(index).ok_or(IndexOutOfRange{ index, range: 0..len }) }
333}
334impl<T> GetMut<usize> for VecDeque<T>
335{
336    #[inline(always)]
337    fn get_mut(&mut self, idx : usize) -> Option<&mut Self::Output> { self.get_mut(idx) }
338}
339
340impl<T> GetManyMut<usize> for VecDeque<T>
341{
342    #[track_caller]
343    #[inline(always)]
344    unsafe fn get_many_unchecked_mut<const N: usize>(&mut self, indices : [usize; N]) -> [&mut Self::Output;N]
345    {
346        unsafe
347        {
348            // Can probably be improved using `self.as_mut_slices()`
349            self.make_contiguous().get_disjoint_unchecked_mut(indices)
350        }
351    }
352    #[inline(always)]
353    fn try_get_many_mut<const N: usize>(&mut self, indices : [usize; N]) -> Result<[&mut Self::Output;N], ManyMutError> { self.make_contiguous().try_get_many_mut(indices) }
354    #[inline(always)]
355    fn get_many_mut<const N: usize>(&mut self, indices: [usize; N]) -> Option<[&mut Self::Output;N]> { self.make_contiguous().get_many_mut(indices) }
356}
357
358
359impl<Idx> TryGet<Idx> for str where Idx : SliceIndex<str> + Clone
360{
361    type Error = IndexOutOfRange<Idx,Range<usize>>;
362    fn try_get(&self, index : Idx) -> Result<&Self::Output, Self::Error>
363    {
364        self.get(index.clone()).ok_or(IndexOutOfRange{ index, range: 0..self.len() })
365    }
366}
367
368impl<Idx> Get<Idx> for str where Idx : SliceIndex<str>
369{
370    type Output = <Self as Index<Idx>>::Output;
371    #[inline(always)]
372    fn get(&self, idx : Idx) -> Option<&Self::Output> { self.get(idx) }
373    #[inline(always)]
374    #[track_caller]
375    unsafe fn get_unchecked(&self, idx : Idx) -> &Self::Output { unsafe { self.get_unchecked(idx) } }
376}
377
378// FIXME : I'm not sure how to delegate it to `str` like delegating the TryGet for `Vec<T>` into the TryGet of `[T]`
379impl<Idx> TryGet<Idx> for String where Idx : SliceIndex<str> + Clone
380{
381    type Error = IndexOutOfRange<Idx,Range<usize>>;
382    fn try_get(&self, index : Idx) -> Result<&Self::Output, Self::Error>
383    {
384        self.get(index.clone()).ok_or(IndexOutOfRange{ index, range: 0..self.len() })
385    }
386}
387impl<Idx> Get<Idx> for String where Idx : SliceIndex<str>
388{
389    type Output = <str as Get<Idx>>::Output;
390    #[inline(always)]
391    fn get(&self, idx : Idx) -> Option<&Self::Output> { self.as_str().get(idx) }
392    #[track_caller]
393    #[inline(always)]
394    unsafe fn get_unchecked(&self, idx : Idx) -> &Self::Output { unsafe { self.as_str().get_unchecked(idx) } }
395}
396
397#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
398#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
399pub struct MissingKey<K>
400{
401    pub key : K,
402}
403impl<K> MissingKey<K>
404{
405    pub fn new(key: K) -> Self { Self { key }}
406}
407
408impl<K,V,S,Q> TryGet<&Q> for HashMap<K,V,S> where K : Borrow<Q>, Q : ?Sized + Hash + Eq + Clone, K: Eq + Hash, S: BuildHasher
409{
410    type Error = MissingKey<Q>;
411
412    fn try_get(&self, key : &Q) -> Result<&Self::Output, Self::Error> {
413        self.get(key).ok_or_else(|| MissingKey::new(key.clone()))
414    }
415}
416
417impl<K,V,S,Q> Get<&Q> for HashMap<K,V,S> where K : Borrow<Q>, Q : ?Sized + Hash + Eq, K: Eq + Hash, S: BuildHasher
418{
419    type Output = V;
420    #[inline(always)]
421    fn get(&self, k: &Q) -> Option<&Self::Output> { self.get(k) }
422}
423
424
425impl<K,V,S,Q> TryGetMut<&Q> for HashMap<K,V,S> where K : Borrow<Q>, Q : ?Sized + Hash + Eq + Clone, K: Eq + Hash, S: BuildHasher
426{
427    #[inline(always)]
428    fn try_get_mut(&mut self, key: &Q) -> Result<&mut Self::Output, Self::Error> where K : Borrow<Q> {
429        self.get_mut(key).ok_or_else(|| MissingKey::new(key.clone()))
430    }
431}
432impl<K,V,S,Q> GetMut<&Q> for HashMap<K,V,S> where K : Borrow<Q>, Q : ?Sized + Hash + Eq, K: Eq + Hash, S: BuildHasher
433{
434    #[inline(always)]
435    fn get_mut(&mut self, k: &Q) -> Option<&mut Self::Output> where K : Borrow<Q> { self.get_mut(k) }
436}
437
438impl<K,V,S,Q> GetManyMut<&Q> for HashMap<K,V,S> where K : Borrow<Q>, Q : ?Sized + Hash + Eq, K: Eq + Hash, S: BuildHasher
439{
440    #[inline(always)]
441    fn try_get_many_mut<const N: usize>(&mut self, keys: [&Q; N]) -> Result<[&mut Self::Output;N], ManyMutError>
442    {
443        // I wanted to check this is the get_many_mut fail, but the borrow checker is complaining for no reason about self being already borrowed.
444        for k in keys
445        {
446            if self.get_mut(k).is_none() { return Err(ManyMutError::IndexOutOfBounds) }
447        }
448
449        if let Some(r) = self.get_many_mut(keys)
450        {
451            return Ok(r);
452        }
453
454        Err(ManyMutError::OverlappingIndices)
455    }
456
457    fn get_many_mut<const N: usize>(&mut self, keys: [&Q; N]) -> Option<[&mut Self::Output;N]>
458    {
459        // Use try_map https://doc.rust-lang.org/std/primitive.array.html#method.try_map when #stabilized
460        // [Option<T>, N] to Option<[T;N]> (same for result)
461        let r = self.get_disjoint_mut(keys);
462
463        if r.iter().any(|x| x.is_none())
464        {
465            None
466        } else
467        {
468            Some(r.map(|x| x.unwrap()))
469        }
470    }
471}
472
473impl<K,V,Q> TryGet<&Q> for BTreeMap<K,V> where K : Borrow<Q>, Q : ?Sized + Ord + Clone, K: Ord
474{
475    type Error = MissingKey<Q>;
476    fn try_get(&self, index : &Q) -> Result<&Self::Output, Self::Error> {
477        self.get(index).ok_or_else(|| MissingKey::new(index.clone()))
478    }
479}
480impl<K,V,Q> Get<&Q> for BTreeMap<K,V> where K : Borrow<Q>, Q : ?Sized + Ord, K: Ord
481{
482    type Output = V;
483    #[inline(always)]
484    fn get(&self, k: &Q) -> Option<&Self::Output> where K : Borrow<Q> { self.get(k) }
485}
486
487impl<K,V,Q> TryGetMut<&Q> for BTreeMap<K,V> where K : Borrow<Q>, Q : ?Sized + Ord + Clone, K: Ord
488{
489    fn try_get_mut(&mut self, index : &Q) -> Result<&mut Self::Output, Self::Error> {
490        self.get_mut(index).ok_or_else(|| MissingKey::new(index.clone()))
491    }
492}
493impl<K,V,Q> GetMut<&Q> for BTreeMap<K,V> where K : Borrow<Q>, Q : ?Sized + Ord, K: Ord
494{
495    #[inline(always)]
496    fn get_mut(&mut self, k: &Q) -> Option<&mut Self::Output> where K : Borrow<Q> { self.get_mut(k) }
497}
498
499
500impl<K,S,Q> TryGet<&Q> for HashSet<K,S> where K : Borrow<Q>, Q : ?Sized + Hash + Eq + Clone, K: Eq + Hash, S: BuildHasher
501{
502    type Error=MissingKey<Q>;
503    fn try_get(&self, index : &Q) -> Result<&Self::Output, Self::Error> {
504          self.get(index).ok_or_else(|| MissingKey::new(index.clone()))
505    }
506}
507impl<K,S,Q> Get<&Q> for HashSet<K,S> where K : Borrow<Q>, Q : ?Sized + Hash + Eq, K: Eq + Hash, S: BuildHasher
508{
509    type Output = K;
510    #[inline(always)]
511    fn get(&self, k: &Q) -> Option<&Self::Output> { self.get(k) }
512}
513
514impl<K,Q> TryGet<&Q> for BTreeSet<K> where K : Borrow<Q>, Q : ?Sized + Ord + Clone, K: Ord
515{
516    type Error=MissingKey<Q>;
517    fn try_get(&self, index : &Q) -> Result<&Self::Output, Self::Error> {
518          self.get(index).ok_or_else(|| MissingKey::new(index.clone()))
519    }
520}
521impl<K,Q> Get<&Q> for BTreeSet<K> where K : Borrow<Q>, Q : ?Sized + Ord, K: Ord
522{
523    type Output = K;
524    #[inline(always)]
525    fn get(&self, k: &Q) -> Option<&Self::Output> { self.get(k) }
526}