macro_rules! destruct_bind {
(@reqr $vv:ident, $var:ident) => { ... };
(@reqr $vv:ident, $var:ident $($vars:tt)+) => { ... };
(@reqr $vv:ident,) => { ... };
(@no-rest $vv:ident) => { ... };
(@rest $rest:ident $vv:ident) => { ... };
(@optvar $vv:ident, $var:ident) => { ... };
(@optvar $vv:ident, $var:ident $($vars:ident)+) => { ... };
(@impl ($($vars:ident)+) = $vv:ident) => { ... };
(@impl ($($vars:ident)* &optional $($optvars:ident)+) = $vv:ident) => { ... };
(@impl ($($vars:ident)* &rest $rest:ident) = $vv:ident) => { ... };
(@impl ($($vars:ident)* &optional $($optvars:ident)+ &rest $rest:ident) = $vv:ident) => { ... };
(($($rest:tt)*) = $vv:ident) => { ... };
}Expand description
Destructures lists and binds the components to separate symbols.
Has a syntax similar to emacs lisp defun parameters.
ยงExample
use tulisp::{destruct_bind, list, TulispObject, Error, ErrorKind};
fn main() -> Result<(), Error> {
// Destruct from a list with just the required item present.
let list1 = list!(
,10.into()
)?;
destruct_bind!((num1 &optional str1 num2) = list1);
assert_eq!(num1.as_int()?, 10);
assert!(str1.null());
assert!(num2.null());
// Destruct from a list with just the required items present.
let list1 = list!(
,10.into()
,"hello".into()
,5.2.into()
)?;
destruct_bind!((num1 str1 num2 &rest other) = list1);
assert_eq!(num1.as_int()?, 10);
assert_eq!(str1.as_string()?, "hello");
assert_eq!(num2.as_float()?, 5.2);
assert!(other.null());
// Destruct from a list with all values present.
let list1 = list!(
,10.into()
,"hello".into()
,5.2.into()
,22.into()
,42.into()
)?;
destruct_bind!((num1 &optional str1 num2 &rest other) = list1);
assert_eq!(num1.as_int()?, 10);
assert_eq!(str1.as_string()?, "hello");
assert_eq!(num2.as_float()?, 5.2);
assert_eq!(other.to_string(), "(22 42)");
Ok(())
}