use std::{
collections::hash_map,
fmt,
ops::{Deref, DerefMut},
};
use crate::Id;
pub struct Row<'a, T> {
pub id: Id<T>,
pub data: &'a T,
}
impl<'a, T> Row<'a, T>
where
T: Clone,
{
pub fn cloned(self) -> OwnedRow<T> {
OwnedRow {
id: self.id,
data: self.data.clone(),
}
}
}
impl<'a, T> Clone for Row<'a, T> {
fn clone(&self) -> Self {
Row {
id: self.id,
data: self.data,
}
}
}
impl<'a, T> Copy for Row<'a, T> {}
impl<'a, T, U> PartialEq<Row<'a, U>> for Row<'a, T>
where
T: PartialEq<U>,
{
fn eq(&self, other: &Row<U>) -> bool {
self.data.eq(&other.data)
}
}
impl<'a, T> Eq for Row<'a, T> where T: Eq {}
impl<'a, T, U> PartialEq<RowMut<'a, U>> for Row<'a, T>
where
T: PartialEq<U>,
{
fn eq(&self, other: &RowMut<U>) -> bool {
self.data.eq(other.data)
}
}
impl<'a, T> Deref for Row<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.data
}
}
impl<'a, T> AsRef<T> for Row<'a, T> {
fn as_ref(&self) -> &T {
self.data
}
}
impl<'a, T> fmt::Debug for Row<'a, T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if f.alternate() {
write!(f, "{}: {:#?}", self.id, self.data)
} else {
write!(f, "{}: {:?}", self.id, self.data)
}
}
}
impl<'a, T> fmt::Display for Row<'a, T>
where
T: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<T as fmt::Display>::fmt(&self.data, f)
}
}
pub struct RowMut<'a, T> {
pub id: Id<T>,
pub data: &'a mut T,
}
impl<'a, T> RowMut<'a, T>
where
T: Clone,
{
pub fn cloned(self) -> OwnedRow<T> {
OwnedRow {
id: self.id,
data: self.data.clone(),
}
}
}
impl<'a, T, U> PartialEq<RowMut<'a, U>> for RowMut<'a, T>
where
T: PartialEq<U>,
{
fn eq(&self, other: &RowMut<U>) -> bool {
self.data.eq(&other.data)
}
}
impl<'a, T> Eq for RowMut<'a, T> where T: Eq {}
impl<'a, T, U> PartialEq<Row<'a, U>> for RowMut<'a, T>
where
T: PartialEq<U>,
{
fn eq(&self, other: &Row<U>) -> bool {
(self.data as &T).eq(other.data)
}
}
impl<'a, T> Deref for RowMut<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.data
}
}
impl<'a, T> DerefMut for RowMut<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.data
}
}
impl<'a, T> AsRef<T> for RowMut<'a, T> {
fn as_ref(&self) -> &T {
self.data
}
}
impl<'a, T> AsMut<T> for RowMut<'a, T> {
fn as_mut(&mut self) -> &mut T {
self.data
}
}
impl<'a, T> fmt::Debug for RowMut<'a, T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if f.alternate() {
write!(f, "{}: {:#?}", self.id, self.data)
} else {
write!(f, "{}: {:?}", self.id, self.data)
}
}
}
impl<'a, T> fmt::Display for RowMut<'a, T>
where
T: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<T as fmt::Display>::fmt(&self.data, f)
}
}
pub struct OwnedRow<T> {
pub id: Id<T>,
pub data: T,
}
impl<T, U> PartialEq<OwnedRow<U>> for OwnedRow<T>
where
T: PartialEq<U>,
{
fn eq(&self, other: &OwnedRow<U>) -> bool {
self.data.eq(&other.data)
}
}
impl<T> Eq for OwnedRow<T> where T: Eq {}
impl<'a, T, U> PartialEq<Row<'a, U>> for OwnedRow<T>
where
T: PartialEq<U>,
{
fn eq(&self, other: &Row<U>) -> bool {
self.data.eq(other.data)
}
}
impl<'a, T, U> PartialEq<RowMut<'a, U>> for OwnedRow<T>
where
T: PartialEq<U>,
{
fn eq(&self, other: &RowMut<U>) -> bool {
self.data.eq(other.data)
}
}
impl<T> Deref for OwnedRow<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T> DerefMut for OwnedRow<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
impl<T> AsRef<T> for OwnedRow<T> {
fn as_ref(&self) -> &T {
&self.data
}
}
impl<T> AsMut<T> for OwnedRow<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.data
}
}
impl<T> fmt::Debug for OwnedRow<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if f.alternate() {
write!(f, "{}: {:#?}", self.id, self.data)
} else {
write!(f, "{}: {:?}", self.id, self.data)
}
}
}
impl<T> fmt::Display for OwnedRow<T>
where
T: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<T as fmt::Display>::fmt(&self.data, f)
}
}
#[derive(Debug)]
pub struct RowIter<'a, T> {
pub(crate) inner: hash_map::Iter<'a, Id<T>, T>,
}
impl<'a, T> Iterator for RowIter<'a, T> {
type Item = Row<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(id, data)| Row { id: *id, data })
}
}
impl<'a, T> Clone for RowIter<'a, T> {
fn clone(&self) -> Self {
RowIter {
inner: self.inner.clone(),
}
}
}
#[derive(Debug)]
pub struct RowIterMut<'a, T> {
pub(crate) inner: hash_map::IterMut<'a, Id<T>, T>,
}
impl<'a, T> Iterator for RowIterMut<'a, T> {
type Item = RowMut<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(id, data)| RowMut { id: *id, data })
}
}
pub trait Idd {
type RowType;
fn id(&self) -> Id<Self::RowType>;
}
impl<T> Idd for Id<T> {
type RowType = T;
fn id(&self) -> Id<Self::RowType> {
*self
}
}
impl<'a, T> Idd for Row<'a, T> {
type RowType = T;
fn id(&self) -> Id<Self::RowType> {
self.id
}
}
impl<'a, T> Idd for RowMut<'a, T> {
type RowType = T;
fn id(&self) -> Id<Self::RowType> {
self.id
}
}