use std::error::Error;
use std::fmt;
pub trait EntryView {
type Key;
type Value;
fn view_key(&self) -> &Self::Key;
fn view_value(&self) -> &Self::Value;
}
pub struct OccupiedError<E, V> {
pub entry: E,
pub value: V,
}
impl<E, V> fmt::Debug for OccupiedError<E, V>
where
E: EntryView<Value = V>,
E::Key: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OccupiedError")
.field("key", self.entry.view_key())
.field("value", &self.value)
.finish()
}
}
impl<E, V> fmt::Display for OccupiedError<E, V>
where
E: EntryView<Value = V>,
E::Key: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"tried to insert {:?}, but key {:?} was already present with {:?}",
self.value,
self.entry.view_key(),
self.entry.view_value(),
)
}
}
impl<E, V> Error for OccupiedError<E, V>
where
E: EntryView<Value = V>,
E::Key: fmt::Debug,
V: fmt::Debug,
{
}