use core::borrow::Borrow;
use core::ops::Deref;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Cond<E, F> {
encodable: E,
condition: F,
}
impl<E, F> Cond<E, F> {
#[inline]
#[must_use]
pub const fn new(encodable: E, condition: F) -> Self
where
F: Fn(&E) -> bool,
{
Self {
encodable,
condition,
}
}
#[inline]
#[must_use]
pub fn into_inner(self) -> (E, F) {
(self.encodable, self.condition)
}
}
impl<E, F> AsRef<E> for Cond<E, F> {
#[inline]
fn as_ref(&self) -> &E {
&self.encodable
}
}
impl<E, F> Borrow<E> for Cond<E, F> {
#[inline]
fn borrow(&self) -> &E {
&self.encodable
}
}
impl<E, F> Deref for Cond<E, F> {
type Target = E;
#[inline]
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl<Encodable, Encoder, F> crate::Encodable<Encoder> for Cond<Encodable, F>
where
Encodable: crate::Encodable<Encoder>,
Encoder: crate::BaseEncoder,
F: Fn(&Encodable) -> bool,
{
type Error = Encodable::Error;
#[inline]
fn encode(&self, encoder: &mut Encoder) -> Result<(), Self::Error> {
if (self.condition)(&self.encodable) {
self.encodable.encode(encoder)
} else {
Ok(())
}
}
}