[types] Add Bools to the mix
This commit is contained in:
parent
4d5fcb69ee
commit
d0441965eb
3 changed files with 29 additions and 16 deletions
|
@ -2,14 +2,19 @@
|
||||||
* Eryn Wells <eryn@erynwells.me>
|
* Eryn Wells <eryn@erynwells.me>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use std::any::Any;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use object::Obj;
|
use std::ops::Deref;
|
||||||
use preds;
|
use object::{Obj, Object};
|
||||||
|
|
||||||
/// The Scheme boolean type. It can be `True` or `False`.
|
/// The Scheme boolean type. It can be `True` or `False`.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Bool { True, False }
|
pub enum Bool { True, False }
|
||||||
|
|
||||||
impl Obj for Bool { }
|
impl Object for Bool {
|
||||||
|
fn as_any(&self) -> &Any { self }
|
||||||
|
fn as_bool(&self) -> Option<&Bool> { Some(self) }
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Bool {
|
impl fmt::Display for Bool {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
@ -20,25 +25,29 @@ impl fmt::Display for Bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl preds::IsBool for Bool {
|
impl PartialEq<Obj> for Bool {
|
||||||
fn is_bool(&self) -> bool { true }
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use object::Object;
|
use super::Bool;
|
||||||
use predicates::{IsBool, IsChar};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn bools_are_bools() {
|
|
||||||
assert_eq!(Object::Bool(false).is_bool(), true);
|
|
||||||
assert_eq!(Object::Bool(false).is_char(), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn equal_bools_are_equal() {
|
fn equal_bools_are_equal() {
|
||||||
assert_eq!(Object::Bool(true), Object::Bool(true));
|
assert_eq!(Bool::True, Bool::True);
|
||||||
assert_eq!(Object::Bool(false), Object::Bool(false));
|
assert_eq!(Bool::False, Bool::False);
|
||||||
assert_ne!(Object::Bool(true), Object::Bool(false));
|
assert_ne!(Bool::True, Bool::False);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
mod bool;
|
||||||
mod object;
|
mod object;
|
||||||
mod pair;
|
mod pair;
|
||||||
mod sym;
|
mod sym;
|
||||||
|
|
||||||
|
pub use bool::Bool;
|
||||||
pub use object::Obj;
|
pub use object::Obj;
|
||||||
pub use pair::Pair;
|
pub use pair::Pair;
|
||||||
pub use sym::Sym;
|
pub use sym::Sym;
|
||||||
|
|
|
@ -32,6 +32,8 @@ pub trait Object:
|
||||||
{
|
{
|
||||||
/// Cast this Object to an Any.
|
/// Cast this Object to an Any.
|
||||||
fn as_any(&self) -> &Any;
|
fn as_any(&self) -> &Any;
|
||||||
|
/// Cast this Object to a Bool if possible.
|
||||||
|
fn as_bool(&self) -> Option<&Bool> { None }
|
||||||
/// Cast this Object to a Pair if possible.
|
/// Cast this Object to a Pair if possible.
|
||||||
fn as_pair(&self) -> Option<&Pair> { None }
|
fn as_pair(&self) -> Option<&Pair> { None }
|
||||||
/// Cast this Object to a Sym if possible.
|
/// Cast this Object to a Sym if possible.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue