Skip to content

Commit 3eadd48

Browse files
authored
refactor!: Switch to simple unsigned 64-bit integer for node IDs (#276)
1 parent 5ae557b commit 3eadd48

File tree

18 files changed

+228
-321
lines changed

18 files changed

+228
-321
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/c/examples/sdl/hello_world.c

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
const char WINDOW_TITLE[] = "Hello world";
1414

15-
static accesskit_node_id WINDOW_ID;
16-
static accesskit_node_id BUTTON_1_ID;
17-
static accesskit_node_id BUTTON_2_ID;
18-
static accesskit_node_id ANNOUNCEMENT_ID;
19-
static accesskit_node_id INITIAL_FOCUS;
15+
const accesskit_node_id WINDOW_ID = 0;
16+
const accesskit_node_id BUTTON_1_ID = 1;
17+
const accesskit_node_id BUTTON_2_ID = 2;
18+
const accesskit_node_id ANNOUNCEMENT_ID = 3;
19+
const accesskit_node_id INITIAL_FOCUS = BUTTON_1_ID;
2020

2121
const accesskit_rect BUTTON_1_RECT = {20.0, 20.0, 100.0, 60.0};
2222

@@ -25,21 +25,10 @@ const accesskit_rect BUTTON_2_RECT = {20.0, 60.0, 100.0, 100.0};
2525
const Sint32 SET_FOCUS_MSG = 0;
2626
const Sint32 DO_DEFAULT_ACTION_MSG = 1;
2727

28-
const bool node_id_cmp(const accesskit_node_id *id1,
29-
const accesskit_node_id *id2) {
30-
return memcmp(id1, id2, sizeof(accesskit_node_id)) == 0;
31-
}
32-
33-
accesskit_node_id *node_id_dup(const accesskit_node_id *src) {
34-
accesskit_node_id *result = malloc(sizeof(accesskit_node_id));
35-
memcpy(result, src, sizeof(accesskit_node_id));
36-
return result;
37-
}
38-
3928
accesskit_node *build_button(accesskit_node_id id, const char *name,
4029
accesskit_node_class_set *classes) {
4130
accesskit_rect rect;
42-
if (node_id_cmp(&id, &BUTTON_1_ID)) {
31+
if (id == BUTTON_1_ID) {
4332
rect = BUTTON_1_RECT;
4433
} else {
4534
rect = BUTTON_2_RECT;
@@ -260,7 +249,7 @@ void window_state_press_button(struct window_state *state,
260249
const struct accesskit_sdl_adapter *adapter,
261250
accesskit_node_id id) {
262251
const char *text;
263-
if (node_id_cmp(&id, &BUTTON_1_ID)) {
252+
if (id == BUTTON_1_ID) {
264253
text = "You pressed button 1";
265254
} else {
266255
text = "You pressed button 2";
@@ -294,7 +283,7 @@ void do_action(const accesskit_action_request *request, void *userdata) {
294283
SDL_zero(event);
295284
event.type = state->event_type;
296285
event.user.windowID = state->window_id;
297-
event.user.data1 = node_id_dup(&request->target);
286+
event.user.data1 = (void *)((uintptr_t)(request->target));
298287
if (request->action == ACCESSKIT_ACTION_FOCUS) {
299288
event.user.code = SET_FOCUS_MSG;
300289
SDL_PushEvent(&event);
@@ -334,11 +323,6 @@ int main(int argc, char *argv[]) {
334323
fprintf(stderr, "Couldn't register user event: (%s)\n", SDL_GetError());
335324
return -1;
336325
}
337-
WINDOW_ID = accesskit_node_id_new(1).value;
338-
BUTTON_1_ID = accesskit_node_id_new(2).value;
339-
BUTTON_2_ID = accesskit_node_id_new(3).value;
340-
ANNOUNCEMENT_ID = accesskit_node_id_new(4).value;
341-
INITIAL_FOCUS = BUTTON_1_ID;
342326

343327
struct window_state state;
344328
window_state_init(&state);
@@ -386,7 +370,7 @@ int main(int argc, char *argv[]) {
386370
switch (event.key.keysym.sym) {
387371
case SDLK_TAB:
388372
window_state_lock(&state);
389-
if (node_id_cmp(&state.focus, &BUTTON_1_ID)) {
373+
if (state.focus == BUTTON_1_ID) {
390374
state.focus = BUTTON_2_ID;
391375
} else {
392376
state.focus = BUTTON_1_ID;
@@ -401,18 +385,19 @@ int main(int argc, char *argv[]) {
401385
window_state_unlock(&state);
402386
break;
403387
}
404-
} else if (event.type == user_event && event.user.windowID == window_id &&
405-
(node_id_cmp(event.user.data1, &BUTTON_1_ID) ||
406-
node_id_cmp(event.user.data1, &BUTTON_2_ID))) {
407-
window_state_lock(&state);
408-
accesskit_node_id *target = event.user.data1;
409-
if (event.user.code == SET_FOCUS_MSG) {
410-
state.focus = *target;
411-
window_state_update_focus(&state, &adapter);
412-
} else if (event.user.code == DO_DEFAULT_ACTION_MSG) {
413-
window_state_press_button(&state, &adapter, *target);
388+
} else if (event.type == user_event && event.user.windowID == window_id) {
389+
accesskit_node_id target =
390+
(accesskit_node_id)((uintptr_t)(event.user.data1));
391+
if (target == BUTTON_1_ID || target == BUTTON_2_ID) {
392+
window_state_lock(&state);
393+
if (event.user.code == SET_FOCUS_MSG) {
394+
state.focus = target;
395+
window_state_update_focus(&state, &adapter);
396+
} else if (event.user.code == DO_DEFAULT_ACTION_MSG) {
397+
window_state_press_button(&state, &adapter, target);
398+
}
399+
window_state_unlock(&state);
414400
}
415-
window_state_unlock(&state);
416401
}
417402
}
418403

bindings/c/examples/windows/hello_world.c

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ const WCHAR CLASS_NAME[] = L"AccessKitTest";
77

88
const WCHAR WINDOW_TITLE[] = L"Hello world";
99

10-
static accesskit_node_id WINDOW_ID;
11-
static accesskit_node_id BUTTON_1_ID;
12-
static accesskit_node_id BUTTON_2_ID;
13-
static accesskit_node_id ANNOUNCEMENT_ID;
14-
static accesskit_node_id INITIAL_FOCUS;
10+
const accesskit_node_id WINDOW_ID = 0;
11+
const accesskit_node_id BUTTON_1_ID = 1;
12+
const accesskit_node_id BUTTON_2_ID = 2;
13+
const accesskit_node_id ANNOUNCEMENT_ID = 3;
14+
const accesskit_node_id INITIAL_FOCUS = BUTTON_1_ID;
1515

1616
const accesskit_rect BUTTON_1_RECT = {20.0, 20.0, 100.0, 60.0};
1717

@@ -20,21 +20,10 @@ const accesskit_rect BUTTON_2_RECT = {20.0, 60.0, 100.0, 100.0};
2020
const uint32_t SET_FOCUS_MSG = WM_USER;
2121
const uint32_t DO_DEFAULT_ACTION_MSG = WM_USER + 1;
2222

23-
const bool node_id_cmp(const accesskit_node_id *id1,
24-
const accesskit_node_id *id2) {
25-
return memcmp(id1, id2, sizeof(accesskit_node_id)) == 0;
26-
}
27-
28-
accesskit_node_id *node_id_dup(const accesskit_node_id *src) {
29-
accesskit_node_id *result = malloc(sizeof(accesskit_node_id));
30-
memcpy(result, src, sizeof(accesskit_node_id));
31-
return result;
32-
}
33-
3423
accesskit_node *build_button(accesskit_node_id id, const char *name,
3524
accesskit_node_class_set *classes) {
3625
accesskit_rect rect;
37-
if (node_id_cmp(&id, &BUTTON_1_ID)) {
26+
if (id == BUTTON_1_ID) {
3827
rect = BUTTON_1_RECT;
3928
} else {
4029
rect = BUTTON_2_RECT;
@@ -122,10 +111,10 @@ accesskit_tree_update *window_state_build_initial_tree(
122111
void do_action(const accesskit_action_request *request, void *userdata) {
123112
HWND window = userdata;
124113
if (request->action == ACCESSKIT_ACTION_FOCUS) {
125-
LPARAM lparam = (LPARAM)node_id_dup(&request->target);
114+
LPARAM lparam = (LPARAM)(request->target);
126115
PostMessage((HWND)window, SET_FOCUS_MSG, 0, lparam);
127116
} else if (request->action == ACCESSKIT_ACTION_DEFAULT) {
128-
LPARAM lparam = (LPARAM)node_id_dup(&request->target);
117+
LPARAM lparam = (LPARAM)(request->target);
129118
PostMessage((HWND)window, DO_DEFAULT_ACTION_MSG, 0, lparam);
130119
}
131120
}
@@ -149,7 +138,7 @@ accesskit_windows_adapter *window_state_get_or_init_accesskit_adapter(
149138
void window_state_press_button(struct window_state *state,
150139
accesskit_node_id id) {
151140
const char *text;
152-
if (node_id_cmp(&id, &BUTTON_1_ID)) {
141+
if (id == BUTTON_1_ID) {
153142
text = "You pressed button 1";
154143
} else {
155144
text = "You pressed button 2";
@@ -238,7 +227,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
238227
} else if (msg == WM_KEYDOWN) {
239228
if (wParam == VK_TAB) {
240229
struct window_state *state = get_window_state(hwnd);
241-
if (node_id_cmp(&state->focus, &BUTTON_1_ID)) {
230+
if (state->focus == BUTTON_1_ID) {
242231
state->focus = BUTTON_2_ID;
243232
} else {
244233
state->focus = BUTTON_1_ID;
@@ -252,21 +241,19 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
252241
return DefWindowProc(hwnd, msg, wParam, lParam);
253242
}
254243
} else if (msg == SET_FOCUS_MSG) {
255-
accesskit_node_id *id = (accesskit_node_id *)lParam;
256-
if (node_id_cmp(id, &BUTTON_1_ID) || node_id_cmp(id, &BUTTON_2_ID)) {
244+
accesskit_node_id id = (accesskit_node_id)lParam;
245+
if (id == BUTTON_1_ID || id == BUTTON_2_ID) {
257246
struct window_state *state = get_window_state(hwnd);
258-
state->focus = *id;
247+
state->focus = id;
259248
bool is_window_focused = state->is_window_focused;
260249
update_focus(hwnd, is_window_focused);
261250
}
262-
free(id);
263251
} else if (msg == DO_DEFAULT_ACTION_MSG) {
264-
accesskit_node_id *id = (accesskit_node_id *)lParam;
265-
if (node_id_cmp(id, &BUTTON_1_ID) || node_id_cmp(id, &BUTTON_2_ID)) {
252+
accesskit_node_id id = (accesskit_node_id)lParam;
253+
if (id == BUTTON_1_ID || id == BUTTON_2_ID) {
266254
struct window_state *window_state = get_window_state(hwnd);
267-
window_state_press_button(window_state, *id);
255+
window_state_press_button(window_state, id);
268256
}
269-
free(id);
270257
} else {
271258
return DefWindowProc(hwnd, msg, wParam, lParam);
272259
}
@@ -314,12 +301,6 @@ int main() {
314301
return 0;
315302
}
316303

317-
WINDOW_ID = accesskit_node_id_new(1).value;
318-
BUTTON_1_ID = accesskit_node_id_new(2).value;
319-
BUTTON_2_ID = accesskit_node_id_new(3).value;
320-
ANNOUNCEMENT_ID = accesskit_node_id_new(4).value;
321-
INITIAL_FOCUS = BUTTON_1_ID;
322-
323304
hwnd = create_window(WINDOW_TITLE, INITIAL_FOCUS);
324305

325306
if (hwnd == NULL) {

bindings/c/src/common.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use paste::paste;
99
use std::{
1010
ffi::{CStr, CString},
1111
mem,
12-
num::NonZeroU128,
1312
os::raw::{c_char, c_void},
1413
ptr, slice,
1514
};
@@ -318,24 +317,7 @@ macro_rules! vec_property_methods {
318317
}
319318
}
320319

321-
/// Call `accesskit_node_id_new` to create this struct.
322-
///
323-
/// If you have to manually populate this, ensure it is not filled up only with zeroes.
324-
#[derive(Clone, Copy)]
325-
#[repr(C)]
326-
pub struct node_id([u8; 16]);
327-
328-
impl From<NodeId> for node_id {
329-
fn from(id: NodeId) -> Self {
330-
Self(id.0.get().to_le_bytes())
331-
}
332-
}
333-
334-
impl From<node_id> for NodeId {
335-
fn from(id: node_id) -> Self {
336-
Self(unsafe { NonZeroU128::new_unchecked(u128::from_le_bytes(id.0)) })
337-
}
338-
}
320+
pub type node_id = u64;
339321

340322
slice_struct! { node_ids, NodeId, node_id }
341323

@@ -860,13 +842,6 @@ vec_property_methods! {
860842
(CustomAction, custom_actions, *mut custom_actions, set_custom_actions, custom_action, push_custom_action, clear_custom_actions)
861843
}
862844

863-
impl node_id {
864-
#[no_mangle]
865-
pub extern "C" fn accesskit_node_id_new(id: u64) -> opt_node_id {
866-
NonZeroU128::new(id as u128).map(NodeId).into()
867-
}
868-
}
869-
870845
impl node_builder {
871846
#[no_mangle]
872847
pub extern "C" fn accesskit_node_builder_new(role: Role) -> *mut node_builder {

common/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ use serde::{
2222
ser::{SerializeMap, SerializeSeq, Serializer},
2323
Deserialize, Serialize,
2424
};
25-
use std::{
26-
collections::BTreeSet,
27-
num::{NonZeroU128, NonZeroU64},
28-
ops::DerefMut,
29-
sync::Arc,
30-
};
25+
use std::{collections::BTreeSet, ops::DerefMut, sync::Arc};
3126
#[cfg(feature = "serde")]
3227
use std::{fmt, mem::size_of_val};
3328

@@ -690,9 +685,7 @@ pub enum TextDecoration {
690685
Wavy,
691686
}
692687

693-
// This is NonZeroU128 because we regularly store Option<NodeId>.
694-
// 128-bit to handle UUIDs.
695-
pub type NodeIdContent = NonZeroU128;
688+
pub type NodeIdContent = u64;
696689

697690
/// The stable identity of a [`Node`], unique within the node's tree.
698691
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -701,10 +694,17 @@ pub type NodeIdContent = NonZeroU128;
701694
#[repr(transparent)]
702695
pub struct NodeId(pub NodeIdContent);
703696

704-
impl From<NonZeroU64> for NodeId {
697+
impl From<NodeIdContent> for NodeId {
698+
#[inline]
699+
fn from(inner: NodeIdContent) -> Self {
700+
Self(inner)
701+
}
702+
}
703+
704+
impl From<NodeId> for NodeIdContent {
705705
#[inline]
706-
fn from(inner: NonZeroU64) -> Self {
707-
Self(inner.into())
706+
fn from(outer: NodeId) -> Self {
707+
outer.0
708708
}
709709
}
710710

consumer/src/lib.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,22 @@ mod tests {
2323
use accesskit::{
2424
Affine, NodeBuilder, NodeClassSet, NodeId, Rect, Role, Tree, TreeUpdate, Vec2,
2525
};
26-
use std::num::NonZeroU128;
2726

2827
use crate::FilterResult;
2928

30-
pub const ROOT_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(1) });
31-
pub const PARAGRAPH_0_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(2) });
32-
pub const STATIC_TEXT_0_0_IGNORED_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(3) });
33-
pub const PARAGRAPH_1_IGNORED_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(4) });
34-
pub const STATIC_TEXT_1_0_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(5) });
35-
pub const PARAGRAPH_2_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(6) });
36-
pub const STATIC_TEXT_2_0_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(7) });
37-
pub const PARAGRAPH_3_IGNORED_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(8) });
38-
pub const EMPTY_CONTAINER_3_0_IGNORED_ID: NodeId =
39-
NodeId(unsafe { NonZeroU128::new_unchecked(9) });
40-
pub const LINK_3_1_IGNORED_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(10) });
41-
pub const STATIC_TEXT_3_1_0_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(11) });
42-
pub const BUTTON_3_2_ID: NodeId = NodeId(unsafe { NonZeroU128::new_unchecked(12) });
43-
pub const EMPTY_CONTAINER_3_3_IGNORED_ID: NodeId =
44-
NodeId(unsafe { NonZeroU128::new_unchecked(13) });
29+
pub const ROOT_ID: NodeId = NodeId(0);
30+
pub const PARAGRAPH_0_ID: NodeId = NodeId(1);
31+
pub const STATIC_TEXT_0_0_IGNORED_ID: NodeId = NodeId(2);
32+
pub const PARAGRAPH_1_IGNORED_ID: NodeId = NodeId(3);
33+
pub const STATIC_TEXT_1_0_ID: NodeId = NodeId(4);
34+
pub const PARAGRAPH_2_ID: NodeId = NodeId(5);
35+
pub const STATIC_TEXT_2_0_ID: NodeId = NodeId(6);
36+
pub const PARAGRAPH_3_IGNORED_ID: NodeId = NodeId(7);
37+
pub const EMPTY_CONTAINER_3_0_IGNORED_ID: NodeId = NodeId(8);
38+
pub const LINK_3_1_IGNORED_ID: NodeId = NodeId(9);
39+
pub const STATIC_TEXT_3_1_0_ID: NodeId = NodeId(10);
40+
pub const BUTTON_3_2_ID: NodeId = NodeId(11);
41+
pub const EMPTY_CONTAINER_3_3_IGNORED_ID: NodeId = NodeId(12);
4542

4643
pub fn test_tree() -> crate::tree::Tree {
4744
let mut classes = NodeClassSet::new();

0 commit comments

Comments
 (0)