Skip to content

Commit 5800da5

Browse files
committed
doc: Explain the vm_type attribute better
@crides does this explain it? Closes #882
1 parent 3e656d1 commit 5800da5

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

book/src/marshalling-types.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,36 @@ code using the `api::typ::make_source` function.
3939

4040
### Using derive macros
4141

42-
Add the `gluon_codegen` crate to your `Cargo.toml` and import it:
42+
Add the `gluon_codegen` crate to your `Cargo.toml` this lets you import and derive the
43+
`VmType`, `Getable`, `Pushable` and `Userdata` traits.
4344

44-
```rust,ignore
45-
#[macro_use]
46-
extern crate gluon_codegen;
47-
```
45+
`VmType`, `Getable` and `Pushable` can be implemented on any type which only consists of types which in turn implements
46+
these traits whereas `Userdata` can be derived for any type as long as it is `Debug + Send + Sync` and has a `'static`
47+
lifetime.
4848

49-
`VmType`, `Getable` and `Pushable` can be derived for types that consist only of types that
50-
already implement the respective traits. In the case of `VmType` you also have to specify
51-
the Gluon type the Rust type maps to, using the `#[gluon(vm_type = "<gluon_type>")]` attribute.
49+
Sometimes when deriving `VmType` you do not want to define a new type. In this case you can use the `vm_type` attribute
50+
to point to another, compatible type. See the marshalling example for the complete source for the examples below.
5251

53-
`Userdata` can be derived for any type as long as it is `Debug + Send + Sync` and has a `'static`
54-
lifetime.
52+
```rust,ignore
53+
// Using `vm_type` to point to compatible type defined in gluon
54+
#[derive(Debug, PartialEq, VmType, Getable)]
55+
#[gluon(vm_type = "std.list.List")]
56+
enum List<T> {
57+
Nil,
58+
Cons(T, Box<List<T>>),
59+
}
60+
61+
// Defines an opaque type with Userdata
62+
#[derive(Userdata, Trace, Clone, Debug, VmType)]
63+
// Lets gluon know that the value can be cloned which can be needed when transferring the value between threads
64+
#[gluon_userdata(clone)]
65+
// Refers to the `WindowHandle` type registered on the Rust side
66+
#[gluon(vm_type = "WindowHandle")]
67+
struct WindowHandle {
68+
id: Arc<u64>,
69+
metadata: Arc<str>,
70+
}
71+
```
5572

5673
### Implementing by hand
5774

@@ -206,7 +223,6 @@ the Gluon side, you can use the [Opaque][] type together with the marker types i
206223
```rust,ignore
207224
// we define Either with type parameters, just like in Gluon
208225
#[derive(Getable, Pushable, VmType)]
209-
#[gluon(vm_type = "examples.either.Either")]
210226
enum Either<L, R> {
211227
Left(L),
212228
Right(R),

examples/marshalling.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ field_decl! { unwrap_b, value, key }
5959

6060
// we define Either with type parameters, just like in Gluon
6161
#[derive(Getable, Pushable, VmType)]
62-
#[gluon(vm_type = "examples.either.Either")]
6362
enum Either<L, R> {
6463
Left(L),
6564
Right(R),

0 commit comments

Comments
 (0)