forked from dtolnay/cxx
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kuecks/issue_87_option
Bindings for Option and std::optional
- Loading branch information
Showing
47 changed files
with
2,665 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{{#title rust::Option<T> — Rust ♡ C++}} | ||
# rust::Option\<T\> | ||
|
||
### Public API: | ||
|
||
```cpp,hidelines=... | ||
// rust/cxx.h | ||
... | ||
...namespace rust { | ||
template <typename T> | ||
class Option final { | ||
public: | ||
Option() noexcept; | ||
Option(Option&&) noexcept; | ||
Option(T&&) noexcept; | ||
~Option() noexcept; | ||
const T *operator->() const; | ||
const T &operator*() const; | ||
T *operator->(); | ||
T &operator*(); | ||
Option<T>& operator=(Option&&) noexcept; | ||
bool has_value() const noexcept; | ||
T& value() noexcept; | ||
void reset(); | ||
void set(T&&) noexcept; | ||
}; | ||
...} // namespace rust | ||
``` | ||
|
||
### Restrictions: | ||
|
||
Option<T> only supports pointer-sized references and Box-es; that is, no | ||
fat pointers like &str (though &String is supported) or Box<[u8]>. On the | ||
C++ side, Option<&T> becomes rust::Option<const T*> (and similar for | ||
mutable references), but the pointer is guaranteed to be non-null if the | ||
Option has a value. Also, you can only pass Options themselves by value. | ||
&Option<T> is not allowed. | ||
|
||
## Example | ||
|
||
```rust,noplayground | ||
// src/main.rs | ||
#[cxx::bridge] | ||
mod ffi { | ||
struct Shared { | ||
v: u32, | ||
} | ||
unsafe extern "C++" { | ||
include!("example/include/example.h"); | ||
fn f(elements: Option<&Shared>); | ||
} | ||
} | ||
fn main() { | ||
let shared = Shared { v: 3 }; | ||
ffi::f(Some(&shared)); | ||
ffi::f(None); | ||
} | ||
``` | ||
|
||
```cpp | ||
// include/example.h | ||
|
||
#pragma once | ||
#include "example/src/main.rs.h" | ||
#include "rust/cxx.h" | ||
|
||
void f(rust::Option<const Shared*>); | ||
``` | ||
```cpp | ||
// src/example.cc | ||
#include "example/include/example.h" | ||
void f(rust::Option<const Shared*> o) { | ||
if (o.has_value()) { | ||
// Pointer is guaranteed to be non-null | ||
std::cout << shared.value()->v << std::endl; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.