You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guide/src/type-conversions.md
+27-9Lines changed: 27 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,6 @@ The type `Value` represents Lisp values:
4
4
- They can be copied around, but cannot outlive the `Env` they come from.
5
5
- They are "proxy values": only useful when converted to Rust values, or used as arguments when calling Lisp functions.
6
6
7
-
Lisp vectors are represented by the type `Vector`, which can be considered a "sub-type" of `Value`.
8
-
9
7
## Converting a Lisp `Value` to Rust
10
8
11
9
This is enabled for types that implement `FromLisp`. Most built-in types are supported. Note that conversion may fail, so the return type is `Result<T>`.
@@ -18,12 +16,7 @@ let s = value.into_rust::<String>()?;
18
16
lets:Option<&str> =value.into_rust()?; // None if Lisp value is nil
19
17
```
20
18
21
-
By default, no utf-8 validation is done when converting strings. This can be enabled through a feature:
22
-
23
-
```toml
24
-
[dependencies.emacs]
25
-
features = ["utf-8-validation"]
26
-
```
19
+
It's better to declare input types for `#[defun]` than calling `.into_rust()`, unless delayed conversion is needed.
27
20
28
21
## Converting a Rust Value to Lisp
29
22
@@ -41,7 +34,9 @@ true.into_lisp(env)?; // t
41
34
false.into_lisp(env)?; // nil
42
35
```
43
36
44
-
## Integer Conversion
37
+
It's better to declare return type for `#[defun]` than calling `.into_lisp(env)`, whenever possible.
38
+
39
+
## Integers
45
40
46
41
Integer conversion is lossless by default, which means that a module will signal an "out of range" `rust-error` in cases such as:
47
42
- A `#[defun]` expecting `u8` gets passed `-1`.
@@ -53,3 +48,26 @@ To disable this behavior, use the `lossy-integer-conversion` feature:
53
48
[dependencies.emacs]
54
49
features = ["lossy-integer-conversion"]
55
50
```
51
+
52
+
## Strings
53
+
54
+
By default, no utf-8 validation is done when converting Lisp strings into Rust strings, because the string data returned by Emacs is guaranteed to be valid utf-8 sequence. If you think you've otherwise encountered an Emacs bug, utf-8 validation can be enabled through a feature:
55
+
56
+
```toml
57
+
[dependencies.emacs]
58
+
features = ["utf-8-validation"]
59
+
```
60
+
61
+
## Vectors
62
+
63
+
Lisp vectors are represented by the type `Vector`, which can be considered a "sub-type" of `Value`.
64
+
65
+
To construct Lisp vectors, use `env.make_vector` and `env.vector`, which are efficient wrappers of Emacs's built-in subroutines `make-vector` and `vector`.
0 commit comments