emacs/types/
mod.rs

1use crate::{symbol, Env, Value, Result};
2
3pub use {user_ptr::Transfer, vector::Vector};
4
5mod integer;
6mod float;
7mod string;
8
9mod user_ptr;
10mod vector;
11
12// XXX: More accurate would be `CloneFromLisp` or `Decode`, but ...
13/// Converting Lisp [`Value`] into a Rust type.
14///
15/// # Implementation
16///
17/// The lifetime parameter is put on the trait itself, instead of the method. This allows it to be
18/// implemented for [`Value`] itself.
19///
20/// [`Value`]: struct.Value.html
21pub trait FromLisp<'e>: Sized {
22    fn from_lisp(value: Value<'e>) -> Result<Self>;
23}
24
25// XXX: More accurate would be `CloneToLisp`, `Encode`, but ...
26/// Converting a Rust type into Lisp [`Value`].
27///
28/// # Implementation
29///
30/// The lifetime parameter is put on the trait itself, instead of the method. This allows the impl
31/// for [`Value`] to simply return the input, instead of having to create a new [`Value`].
32///
33/// [`Value`]: struct.Value.html
34pub trait IntoLisp<'e> {
35    fn into_lisp(self, env: &'e Env) -> Result<Value<'e>>;
36}
37
38impl<'e> FromLisp<'e> for Value<'e> {
39    #[inline(always)]
40    fn from_lisp(value: Value<'e>) -> Result<Value<'_>> {
41        Ok(value)
42    }
43}
44
45impl<'e> IntoLisp<'e> for Value<'e> {
46    #[inline(always)]
47    fn into_lisp(self, _: &'e Env) -> Result<Value<'_>> {
48        Ok(self)
49    }
50}
51
52impl<'e, T: FromLisp<'e>> FromLisp<'e> for Option<T> {
53    fn from_lisp(value: Value<'e>) -> Result<Self> {
54        if value.is_not_nil() {
55            Ok(Some(<T as FromLisp>::from_lisp(value)?))
56        } else {
57            Ok(None)
58        }
59    }
60}
61
62impl<'e, T: IntoLisp<'e>> IntoLisp<'e> for Option<T> {
63    fn into_lisp(self, env: &'e Env) -> Result<Value<'_>> {
64        match self {
65            Some(t) => t.into_lisp(env),
66            None => symbol::nil.into_lisp(env),
67        }
68    }
69}
70
71impl IntoLisp<'_> for () {
72    fn into_lisp(self, env: &Env) -> Result<Value<'_>> {
73        symbol::nil.into_lisp(env)
74    }
75}
76
77impl IntoLisp<'_> for bool {
78    fn into_lisp(self, env: &Env) -> Result<Value<'_>> {
79        if self {
80            symbol::t.into_lisp(env)
81        } else {
82            symbol::nil.into_lisp(env)
83        }
84    }
85}