xmelt 0.2.0

A serialization/deserialization framework for XML
Documentation
// type DynRestriction<'a, T> = Box<dyn crate::AsRestriction<<<T as crate::Freeze<'a>>::Kind as crate::Restriction<'a>>::Kind>>;

use crate::DynRestriction;

pub trait Container<T>
	where T: for<'freeze> crate::Freeze<'freeze>
{

	/// Insert value into container. Return true is container can take one value at most and already contained a value
	// fn add<'a>(&mut self, value: T, restrictions: Vec<Box<dyn crate::AsRestriction<<<T as crate::Type<'a>>::Kind as crate::Restriction<'a>>::Kind>>>) -> bool;
	fn add<'freeze>(&mut self, value: T, restrictions: Vec<DynRestriction<'freeze, T>>) -> bool;
	// fn add<'a>(&mut self, value: T, restrictions: Vec<Box<impl crate::AsRestriction<<<T as crate::Type<'a>>::Kind as crate::Restriction<'a>>::Kind>>>) -> bool;
	// fn add<'a>(&mut self, value: T, restrictions: Vec<impl crate::AsRestriction<<<T as crate::Type<'a>>::Kind as crate::Restriction<'a>>::Kind>>) -> bool;

	/// Check if countainer is full
	fn is_ready(&self) -> bool {
		false
	}

	fn len(&self) -> usize;

	/// Initialise container
	fn init() -> Self where Self: Sized;
}

impl<T> Container<T> for Option<T>
	where T: for<'freeze> crate::Freeze<'freeze>
{
	fn add<'freeze>(&mut self, value: T, restrictions: Vec<DynRestriction<'freeze, T>>) -> bool {
		self.replace(value).is_some()
	}

	fn is_ready(&self) -> bool {
		self.is_some()
	}

	fn len(&self) -> usize {
		if self.is_none() {
			0
		} else {
			1
		}
	}

	fn init() -> Self
		where Self: Sized
	{
		None
	}
}

impl<T> Container<T> for Vec<T>
	where T: for<'freeze> crate::Freeze<'freeze>
{
	fn add<'freeze>(&mut self, value: T, restrictions: Vec<DynRestriction<'freeze, T>>) -> bool {
		self.push(value);
		return false
	}

	fn len(&self) -> usize {
		self.len()
	}

	fn init() -> Self
		where Self: Sized
	{
		Vec::new()
	}
}

/*impl Container<String> for String {
	fn add<'a>(&mut self, value: String, restrictions: Vec<DynRestriction<'a, String>>) -> bool {
		self.push_str(&value);
		return false
	}

	fn init() -> Self
		where Self: Sized
	{
		Self::new()
	}

	fn len(&self) -> usize {
		0
	}
}*/

pub trait Map<K, V>
	where V: for<'freeze> crate::Freeze<'freeze>
{

	/// Insert value into map. Return true is container can take one value at most and already contained a value
	// fn add<'a>(&mut self, value: T, restrictions: Vec<Box<dyn crate::AsRestriction<<<T as crate::Type<'a>>::Kind as crate::Restriction<'a>>::Kind>>>) -> bool;
	fn add<'freeze>(&mut self, key: K, value: V, restrictions: Vec<DynRestriction<'freeze, V>>) -> bool;

	/// Check if countainer is full
	fn is_ready(&self) -> bool {
		false
	}

	fn len(&self) -> usize;

	/// Initialise container
	fn init() -> Self where Self: Sized;
}

impl<K, V> Map<K, V> for std::collections::HashMap<K, V>
	where
		K: Eq + std::hash::Hash,
		V: for<'freeze> crate::Freeze<'freeze>
{
	fn add<'freeze>(&mut self, key: K, value: V, restrictions: Vec<DynRestriction<'freeze, V>>) -> bool {
		self.insert(key, value);
		return false
	}

	fn len(&self) -> usize {
		self.len()
	}

	fn init() -> Self
		where Self: Sized
	{
		Self::new()
	}
}