use std::borrow::Borrow; use crate::{AList, Bookmark}; pub trait Get { fn get(self, alist: &AList) -> Option<&V>; } impl<'q, Q, K, V> Get for &mut Bookmark<'q, Q, K, V> where K: Borrow, Q: Eq + ?Sized, { fn get(self, alist: &AList) -> Option<&V> { if !alist.is_valid(self) { self.position = alist.position(self.key())?; } let (_, v) = &alist.pairs[self.position]; Some(v) } } impl Get for &Q where K: Borrow, Q: Eq + ?Sized, { fn get(self, alist: &AList) -> Option<&V> { alist.find(self).map(|(_, v)| v) } } pub trait GetMut { fn get_mut(self, alist: &mut AList) -> Option<&mut V>; } impl<'q, Q, K, V> GetMut for &mut Bookmark<'q, Q, K, V> where K: Borrow, Q: Eq + ?Sized, { fn get_mut(self, alist: &mut AList) -> Option<&mut V> { if !alist.is_valid(self) { self.position = alist.position(self.key())?; } let (_, v) = &mut alist.pairs[self.position]; Some(v) } } impl GetMut for &Q where K: Borrow, Q: Eq + ?Sized, { fn get_mut(self, alist: &mut AList) -> Option<&mut V> { alist.find_mut(self).map(|(_, v)| v) } } pub trait GetKeyValue { fn get_key_value(self, alist: &AList) -> Option<(&K, &V)>; } impl<'q, Q, K, V> GetKeyValue for &mut Bookmark<'q, Q, K, V> where K: Borrow, Q: Eq + ?Sized, { fn get_key_value(self, alist: &AList) -> Option<(&K, &V)> { if !alist.is_valid(self) { self.position = alist.position(self.key())?; } let (k, v) = &alist.pairs[self.position]; Some((k, v)) } } impl GetKeyValue for &Q where K: Borrow, Q: Eq + ?Sized, { fn get_key_value(self, alist: &AList) -> Option<(&K, &V)> { alist.find(self) } } pub trait GetKeyValueMut { fn get_key_value_mut(self, alist: &mut AList) -> Option<(&K, &mut V)>; } impl<'q, Q, K, V> GetKeyValueMut for &mut Bookmark<'q, Q, K, V> where K: Borrow, Q: Eq + ?Sized, { fn get_key_value_mut(self, alist: &mut AList) -> Option<(&K, &mut V)> { if !alist.is_valid(self) { self.position = alist.position(self.key())?; } let (k, v) = &mut alist.pairs[self.position]; Some((&*k, v)) } } impl GetKeyValueMut for &Q where K: Borrow, Q: Eq + ?Sized, { fn get_key_value_mut(self, alist: &mut AList) -> Option<(&K, &mut V)> { alist.find_mut(self) } } pub trait ContainsKey { fn contains_key(self, alist: &AList) -> bool; } impl<'q, Q, K, V> ContainsKey for &mut Bookmark<'q, Q, K, V> where K: Borrow, Q: Eq + ?Sized, { fn contains_key(self, alist: &AList) -> bool { if !alist.is_valid(self) { match alist.position(self.key()) { Some(position) => self.position = position, None => return false, } } true } } impl ContainsKey for &Q where K: Borrow, Q: Eq + ?Sized, { fn contains_key(self, alist: &AList) -> bool { alist.pairs.iter().any(|(k, _)| k.borrow() == self) } } pub trait Remove { fn remove(self, alist: &mut AList) -> Option; } impl<'q, Q, K, V> Remove for Bookmark<'q, Q, K, V> where K: Borrow, Q: Eq + ?Sized, { fn remove(mut self, alist: &mut AList) -> Option { if !alist.is_valid(&self) { self.position = alist.position(self.key())?; } let (_, v) = alist.pairs.swap_remove(self.position); Some(v) } } impl Remove for &Q where K: Borrow, Q: Eq + ?Sized, { fn remove(self, alist: &mut AList) -> Option { alist.position(self).map(|position| { let (_, v) = alist.pairs.swap_remove(position); v }) } } pub trait RemoveEntry { fn remove_entry(self, alist: &mut AList) -> Option<(K, V)>; } impl<'q, Q, K, V> RemoveEntry for Bookmark<'q, Q, K, V> where K: Borrow, Q: Eq + ?Sized, { fn remove_entry(mut self, alist: &mut AList) -> Option<(K, V)> { if !alist.is_valid(&self) { self.position = alist.position(self.key())?; } Some(alist.pairs.swap_remove(self.position)) } } impl RemoveEntry for &Q where K: Borrow, Q: Eq + ?Sized, { fn remove_entry(self, alist: &mut AList) -> Option<(K, V)> { alist .position(self) .map(|position| alist.pairs.swap_remove(position)) } }