#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use crate::Empty;
#[cfg_attr(
feature = "serde",
derive(Deserialize, Serialize),
serde(bound(
serialize = "T: Serialize + Clone",
deserialize = "T: Deserialize<'de>"
)),
serde(into = "Vec<T>", try_from = "Vec<T>")
)]
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EVec<T> {
vector: PhantomData<T>,
}
impl<T> EVec<T> {
pub const fn contains(&self, _: &T) -> bool {
false
}
pub fn from_vec(vec: Vec<T>) -> Option<EVec<T>> {
vec.is_empty().then(|| EVec::new())
}
pub const fn is_empty(&self) -> bool {
true
}
pub fn iter(&self) -> Empty<&T> {
Empty::new()
}
pub const fn len(&self) -> usize {
0
}
pub fn new() -> EVec<T> {
EVec {
vector: PhantomData,
}
}
}
impl<T> From<EVec<T>> for Vec<T> {
fn from(_: EVec<T>) -> Self {
Vec::new()
}
}
impl<T> std::fmt::Debug for EVec<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let v: [usize; 0] = [];
v.fmt(f)
}
}
impl<T> TryFrom<Vec<T>> for EVec<T> {
type Error = &'static str;
fn try_from(value: Vec<T>) -> Result<Self, Self::Error> {
if value.is_empty() {
Ok(EVec::new())
} else {
Err("Cannot convert a non-empty vector into an empty one")
}
}
}
impl<T> IntoIterator for EVec<T> {
type Item = T;
type IntoIter = Empty<T>;
fn into_iter(self) -> Self::IntoIter {
Empty::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn eq() {
let a: EVec<usize> = EVec::new();
let b = EVec::new();
assert_eq!(a, b);
}
#[test]
fn ord() {
let a: EVec<usize> = EVec::new();
let b = EVec::new();
assert!(!(a < b));
assert!(!(a > b));
}
#[cfg(feature = "serde")]
mod serialize {
use crate::EVec;
#[test]
fn roundtrip() {
let v: EVec<usize> = EVec::new();
let j = serde_json::to_string(&v).unwrap();
let r = serde_json::from_str(&j).unwrap();
assert_eq!(v, r);
}
}
}