sibil/types/src/bool.rs

64 lines
1.3 KiB
Rust
Raw Normal View History

2017-04-22 09:30:28 -07:00
/* types/src/bool.rs
2017-04-03 14:51:39 -04:00
* Eryn Wells <eryn@erynwells.me>
*/
2018-08-26 09:52:17 -07:00
use std::any::Any;
use std::fmt;
2018-08-26 09:52:17 -07:00
use std::ops::Deref;
use object::{Obj, Object};
/// The Scheme boolean type. It can be `True` or `False`.
2018-08-26 09:52:17 -07:00
#[derive(Debug, PartialEq)]
pub enum Bool { True, False }
2018-08-26 09:52:17 -07:00
impl Object for Bool {
fn as_any(&self) -> &Any { self }
fn as_bool(&self) -> Option<&Bool> { Some(self) }
}
impl fmt::Display for Bool {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Bool::True => write!(f, "#t"),
Bool::False => write!(f, "#f")
2017-04-22 09:30:28 -07:00
}
2017-04-03 14:51:39 -04:00
}
}
2018-08-26 17:41:50 -07:00
impl From<bool> for Bool {
fn from(value: bool) -> Self {
if value {
Bool::True
} else {
Bool::False
}
}
}
2018-08-26 09:52:17 -07:00
impl PartialEq<Obj> for Bool {
fn eq(&self, rhs: &Obj) -> bool {
match rhs {
Obj::Null => false,
Obj::Ptr(ref inner) => {
if let Some(rhs_bool) = inner.deref().as_bool() {
self == rhs_bool
} else {
false
}
}
}
}
}
#[cfg(test)]
mod tests {
2018-08-26 09:52:17 -07:00
use super::Bool;
2017-04-03 16:44:04 -04:00
#[test]
fn equal_bools_are_equal() {
2018-08-26 09:52:17 -07:00
assert_eq!(Bool::True, Bool::True);
assert_eq!(Bool::False, Bool::False);
assert_ne!(Bool::True, Bool::False);
}
}