use std::cell::Ref; use std::ops::{Deref, DerefMut}; pub trait Sortable { type Item; fn sort_by_key_b(&mut self, func: Func) where Func: FnMut(&Self::Item) -> Key, Key: Ord; } impl Sortable for [Item] { type Item = Item; fn sort_by_key_b(&mut self, func: Func) where Func: FnMut(&Self::Item) -> Key, Key: Ord, { self.sort_by_key(func) } } impl Sortable for [Item; LENGTH] { type Item = Item; fn sort_by_key_b(&mut self, func: Func) where Func: FnMut(&Self::Item) -> Key, Key: Ord, { self.sort_by_key(func) } } impl Sortable for Vec { type Item = Item; fn sort_by_key_b(&mut self, func: Func) where Func: FnMut(&Self::Item) -> Key, Key: Ord, { self.sort_by_key(func) } } #[derive(Debug)] pub struct RefCellRefMap<'a, Value: 'a, MappedValue: 'a> { mapped_value: MappedValue, _refcell_ref: Ref<'a, Value>, } impl<'a, Value: 'a, MappedValue: 'a> RefCellRefMap<'a, Value, MappedValue> { pub fn new( refcell_ref: Ref<'a, Value>, map_fn: impl Fn(&'a Value) -> MappedValue, ) -> Self { // Convert the lifetime to 'static. This is necessary to make the compiler not // complain about refcell_ref being moved // // SAFETY: This is fine since we pass it to map_fn with the original lifetime 'a let val_ref = unsafe { &*(&*refcell_ref as *const Value) }; let mapped_value = map_fn(val_ref); Self { mapped_value, _refcell_ref: refcell_ref, } } } impl<'a, Value: 'a, MappedValue: 'a> Deref for RefCellRefMap<'a, Value, MappedValue> { type Target = MappedValue; fn deref(&self) -> &Self::Target { &self.mapped_value } } impl<'a, Value: 'a, MappedValue: 'a> DerefMut for RefCellRefMap<'a, Value, MappedValue> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.mapped_value } }