Skip to main content

BooleanExpr

Enum BooleanExpr 

Source
pub enum BooleanExpr<Var> {
    False,
    True,
    Var(Var),
    Neg(Box<Self>),
    And(Box<Self>, Box<Self>),
    Or(Box<Self>, Box<Self>),
    Xor(Box<Self>, Box<Self>),
}
Expand description

Boolean expression.

Variants§

§

False

Boolean value False.

§

True

Boolean value True.

§

Var(Var)

Variable in an expression.

§

Neg(Box<Self>)

Unary negation of an expression.

§

And(Box<Self>, Box<Self>)

Binary Boolean AND of two expressions.

§

Or(Box<Self>, Box<Self>)

Binary Boolean OR of two expressions.

§

Xor(Box<Self>, Box<Self>)

Binary Boolean XOR of two expressions.

Implementations§

Source§

impl<V> BooleanExpr<V>

Source

pub fn eval<B>(&self, value_fn: &impl Fn(&V) -> B) -> B
where B: Not<Output = B> + BitAnd<Output = B> + BitOr<Output = B> + BitXor<Output = B> + Default,

Evaluate the expression. Variable are passed with the callback function value_fn. The type B usually represents Boolean logic values. For example bool can be used.

Note that B::default() is required to return the Boolean value false.

§Example
use liberty_io::boolean::*;

// Create a simple boolean expression.
let a = BooleanExpr::Var(0);
let b = BooleanExpr::Var(1);
let expr = a ^ b;

let values = [true, false];
assert_eq!(expr.eval(&|var| values[*var]), true);

let values = [true, true];
assert_eq!(expr.eval(&|var| values[*var]), false);
Source

pub fn substitute_variable_names<V2>( &self, substitution: &impl Fn(&V) -> V2, ) -> BooleanExpr<V2>

Substitute variables in the expression. Can also be used to change the data-type of the variables.

§Example
use liberty_io::boolean::*;

// Create a simple boolean expression.
let a = BooleanExpr::Var(0);
let b = BooleanExpr::Var(1);
let expr = a & b;

// Define the variable mapping used for the substitution.
let substitution = |var: &usize| -> String {
   match var {
      0 => "a".into(),
      1 => "b".into(),
      _ => panic!("no such variable")
   }
};

// Apply the substitution.
let substituted = expr.substitute_variable_names(&substitution);
Source

pub fn substitute_variables<V2>( &self, substitution: &impl Fn(&V) -> BooleanExpr<V2>, ) -> BooleanExpr<V2>

Substitute all variables by an expression.

§Example

Replace some variables by a constant:

use liberty_io::boolean::*;

// Create a simple boolean expression.
let a = BooleanExpr::Var("a");
let b = BooleanExpr::Var("b");
let expr = a & b;

// Define the variable mapping used for the substitution.
let substitution = |&var: &&'static str| -> BooleanExpr<&'static str> {
   match var {
      "a" => BooleanExpr::False, // Replace "a".
      var => BooleanExpr::Var(var) // Don't do anything else for all other variables.
   }
};

// Apply the substitution.
let substituted = expr.substitute_variables(&substitution);

Trait Implementations§

Source§

impl<V> Add for BooleanExpr<V>

Source§

type Output = BooleanExpr<V>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<V> BitAnd for BooleanExpr<V>

Source§

type Output = BooleanExpr<V>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl<V> BitOr for BooleanExpr<V>

Source§

type Output = BooleanExpr<V>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl<V> BitXor for BooleanExpr<V>

Source§

type Output = BooleanExpr<V>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<Var: Clone> Clone for BooleanExpr<Var>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Var: Debug> Debug for BooleanExpr<Var>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<V> Display for BooleanExpr<V>
where V: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Var: Eq> Eq for BooleanExpr<Var>

Source§

impl<V> From<bool> for BooleanExpr<V>

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl<Var: Hash> Hash for BooleanExpr<Var>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<V> Mul for BooleanExpr<V>

Source§

type Output = BooleanExpr<V>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<V> Not for BooleanExpr<V>

Source§

type Output = BooleanExpr<V>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation using logical or bitwise negation. Read more
Source§

impl<Var: PartialEq> PartialEq for BooleanExpr<Var>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<Var: PartialEq> StructuralPartialEq for BooleanExpr<Var>

Auto Trait Implementations§

§

impl<Var> Freeze for BooleanExpr<Var>
where Var: Freeze, Box<BooleanExpr<Var>>: Freeze,

§

impl<Var> RefUnwindSafe for BooleanExpr<Var>

§

impl<Var> Send for BooleanExpr<Var>
where Var: Send, Box<BooleanExpr<Var>>: Send,

§

impl<Var> Sync for BooleanExpr<Var>
where Var: Sync, Box<BooleanExpr<Var>>: Sync,

§

impl<Var> Unpin for BooleanExpr<Var>
where Var: Unpin, Box<BooleanExpr<Var>>: Unpin,

§

impl<Var> UnsafeUnpin for BooleanExpr<Var>

§

impl<Var> UnwindSafe for BooleanExpr<Var>
where Var: UnwindSafe, Box<BooleanExpr<Var>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.