Skip to content

Commit

Permalink
mention Option in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Aug 20, 2024
1 parent 051e367 commit 99c71c5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
32 changes: 26 additions & 6 deletions src/option.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Option types in Diplomat are relatively straightforward, you simply use `Option<T>` and it turns into the idiomatic equivalent over FFI.

`Option<T>` currently only works when wrapping reference types (`Box<OpaqueType>` and `&OpaqueType`), or in return type position:
`Option<T>` currently only works when wrapping reference types (`Box<OpaqueType>` and `&OpaqueType`), wrapping structs/enums/primitives, or in return type position:

```rust
#[diplomat::bridge]
Expand All @@ -16,17 +16,37 @@ mod ffi {
Some(Box::new(Thingy))
}

// works in return position, but not elsewhere
pub fn make_option() -> Option<u8> {
Some(1)
pub fn increment_option(x: Option<u8>) -> Option<u8> {
x.map(|inner| inner + 1)
}
}
}
```

In C++ `maybe_create` will return a `std::optional<std::unique_ptr<Thingy>>`, and in JS it will return a potentially-null object.

`make_option` will have similar behavior, returning `std::optional<uint8_t>` and an integer-or-null in JS.
`make_option` will have similar behavior, returning `std::optional<uint8_t>` and an integer-or-null in JS. It will accept `std::optional<uint8_t>` in C++ and null-check the parameter in JS.

## DiplomatOption

In the future, `Option<T>` will be supported for most types `T` ([#246](https://github.com/rust-diplomat/diplomat/issues/246)).
`Option<T>` is FFI-safe for reference types but not for other arbitrary types. When used in function parameters, Diplomat will automatically use FFI-safe types over the boundary, however with structs layout concerns prevent automatically doing this. Instead, if you wish to use an `Option<T>` in a struct (for struct, enum, or primitive `T`), use `DiplomatOption<T>`

```rust
#[diplomat::bridge]
mod ffi {
use diplomat_runtime::DiplomatOption;

#[diplomat::opaque]
pub struct MyOpaque(u8);

pub enum MyEnum {
Foo, Bar
}

pub struct MyStruct<'a> {
a: DiplomatOption<u8>,
b: DiplomatOption<MyEnum>,
c: Option<&'a MyOpaque>
}
}
```
7 changes: 4 additions & 3 deletions src/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ Diplomat only supports a small set of types that can be passed over FFI.
- `&DiplomatStr`: An unvalidated string expected to be UTF-8.
- `&DiplomatStr16`: An unvalidated string expected to be UTF-16.
- [`DiplomatWriteable`](./writeable.md) for returning strings. This needs to be the last parameter of the method.
- [`Option<&T>` ,`Option<Box<T>>`](./option.md) of opaque types
- [`Result<T, E>` and `Option<T>`](./result.md) in return values
- [`Option<&T>` ,`Option<Box<T>>`](./option.md) of opaque types, `Option<T>` of structs, enums, and primitives
- Callbacks in parameters (Undocumented in the book, but implemented in some backends. See [tracking issue](https://github.com/rust-diplomat/diplomat/issues/146))
- [`Result<T, E>` in return values
- `()` as a `Result` `Ok`/`Error` type, or as a return value
- Custom types
- Custom [opaque types](./opaque.md) (passed as references or via `Box<T>`)
- Custom [structs and C-like enums](./structs.md)

More types can be supported in the future (We have issues for [callbacks](https://github.com/rust-diplomat/diplomat/issues/146) and [full option types](https://github.com/rust-diplomat/diplomat/issues/246))
More types can be supported in the future (We have an issue open for [traits](https://github.com/rust-diplomat/diplomat/pull/621))

The _main_ distinction to keep track of is between "opaque types" and "structs": opaque types are for when you want to wrap a Rust object that has its own semantics, whereas "structs" are for when you want to transparently pass around multiple values at once (usually when you want to make an options struct as an argument, or return multiple values at once).

0 comments on commit 99c71c5

Please sign in to comment.