use alloc::vec::Vec;
use core::fmt;
#[derive(Debug)]
pub struct DuplicateItem<T, D = T> {
new: T,
duplicates: Vec<D>,
}
impl<T, D> DuplicateItem<T, D> {
#[doc(hidden)]
pub fn __internal_new(new: T, duplicates: Vec<D>) -> Self {
DuplicateItem { new, duplicates }
}
#[inline]
pub fn new_item(&self) -> &T {
&self.new
}
#[inline]
pub fn duplicates(&self) -> &[D] {
&self.duplicates
}
pub fn into_parts(self) -> (T, Vec<D>) {
(self.new, self.duplicates)
}
}
impl<T: Clone> DuplicateItem<T, &T> {
pub fn into_owned(self) -> DuplicateItem<T> {
DuplicateItem {
new: self.new,
duplicates: self.duplicates.into_iter().cloned().collect(),
}
}
}
impl<T: fmt::Debug, D: fmt::Debug> fmt::Display for DuplicateItem<T, D> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"new item: {:?} conflicts with existing: {:?}",
self.new, self.duplicates
)
}
}
impl<T: fmt::Debug, D: fmt::Debug> core::error::Error for DuplicateItem<T, D> {}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TryReserveError {
kind: TryReserveErrorKind,
}
#[derive(Clone, PartialEq, Eq, Debug)]
enum TryReserveErrorKind {
CapacityOverflow,
AllocError {
layout: core::alloc::Layout,
},
}
impl TryReserveError {
pub(crate) fn from_hashbrown(error: hashbrown::TryReserveError) -> Self {
let kind = match error {
hashbrown::TryReserveError::CapacityOverflow => {
TryReserveErrorKind::CapacityOverflow
}
hashbrown::TryReserveError::AllocError { layout } => {
TryReserveErrorKind::AllocError { layout }
}
};
Self { kind }
}
}
impl fmt::Display for TryReserveError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.kind {
TryReserveErrorKind::CapacityOverflow => {
write!(f, "capacity overflow")
}
TryReserveErrorKind::AllocError { .. } => {
write!(f, "memory allocation failed")
}
}
}
}
impl core::error::Error for TryReserveError {}