Skip to content

Commit 0141b59

Browse files
committed
Improve type conversion doc
See #28
1 parent 69baf14 commit 0141b59

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

guide/src/type-conversions.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ The type `Value` represents Lisp values:
44
- They can be copied around, but cannot outlive the `Env` they come from.
55
- They are "proxy values": only useful when converted to Rust values, or used as arguments when calling Lisp functions.
66

7-
Lisp vectors are represented by the type `Vector`, which can be considered a "sub-type" of `Value`.
8-
97
## Converting a Lisp `Value` to Rust
108

119
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>()?;
1816
let s: Option<&str> = value.into_rust()?; // None if Lisp value is nil
1917
```
2018

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.
2720

2821
## Converting a Rust Value to Lisp
2922

@@ -41,7 +34,9 @@ true.into_lisp(env)?; // t
4134
false.into_lisp(env)?; // nil
4235
```
4336

44-
## Integer Conversion
37+
It's better to declare return type for `#[defun]` than calling `.into_lisp(env)`, whenever possible.
38+
39+
## Integers
4540

4641
Integer conversion is lossless by default, which means that a module will signal an "out of range" `rust-error` in cases such as:
4742
- A `#[defun]` expecting `u8` gets passed `-1`.
@@ -53,3 +48,26 @@ To disable this behavior, use the `lossy-integer-conversion` feature:
5348
[dependencies.emacs]
5449
features = ["lossy-integer-conversion"]
5550
```
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`.
66+
67+
```rust
68+
env.make_vector(5, ())?;
69+
70+
env.vector([1, 2, 3])?;
71+
72+
env.vector((1, "x", true))?;
73+
```

0 commit comments

Comments
 (0)