Skip to content

Commit c415fe3

Browse files
committed
meta: create directory for developer documentation
1 parent cdd53ac commit c415fe3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

CONTRIBUTING.md renamed to CONTRIBUTING/CONTRIBUTING.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ from slowing down your PR.
3434
* Mark unsafe functions as `unsafe`. This includes _anything_ with potential
3535
undefined behavior, not just memory unsafety.
3636
* Document your code! This is the number one holdup for pull requests, especially
37-
large ones. Don't forget to insert appropriate entries into the changelog.
37+
large ones. This includes adding new concepts to the CONTRIBUTING directory (developer
38+
documentation).
39+
* Insert appropriate entries into the changelog.
3840
* If implementing a new feature from `ui`, please mention the stability of that
3941
feature in your pull request. We are fine with implementing unstable APIs from
4042
`ui`, but it's important to mark such APIs as unstable.

CONTRIBUTING/callbacks.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Callbacks with libui
2+
3+
libui provides a mechanism through which each callback can be passed arbitrary data. (An
4+
untyped buffer.)
5+
IUI uses this mechanism to provide a C "wrapper" function which converts any raw libui
6+
types into IUI Rust types, and does sanity checking.
7+
8+
We do this with the functions `callback_helpers::to_heap_ptr`, which `Box`es up a Rust
9+
type and returns a pointer to the allocated memory, and `callback_helpers::from_void_ptr`,
10+
which reconstitutes the Rust type (specified using generics). These functions are `unsafe`
11+
for obvious reasons, and can also leak memory if used improperly.
12+
13+
Their intended use is for `to_heap_ptr` to turn a Rust function into a bag of bits and for
14+
`from_void_ptr` to reconstitute that function into the _exact same type_, which we ensure
15+
by "generic locking" the user and wrapper functions, like so:
16+
17+
```rust
18+
fn on_whatever<'ctx, F: FnMut(&Whatever)>(&mut self, _ctx: &'ctx UI, callback: F) {
19+
20+
fn c_callback<G: FnMut(&Whatever)> { /* ... do stuff ... */ }
21+
22+
ui_sys::uiWhateverOnWhatever(/* ... */, c_callback::<F>);
23+
}
24+
```
25+
26+
This is somewhat verbose but ensures that the types do not deviate, which would be unsafe.
27+
28+
Callbacks should be named `on_event` where `event` is, for instance, `clicked` or
29+
`closing`.
30+

0 commit comments

Comments
 (0)