Skip to content

Commit f885b39

Browse files
authored
feat: Add functions to get string representation of opaque types (#56)
1 parent c441ef4 commit f885b39

File tree

6 files changed

+118
-20
lines changed

6 files changed

+118
-20
lines changed

include/accesskit.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,6 +2000,11 @@ struct accesskit_node *accesskit_node_new(accesskit_role role);
20002000

20012001
void accesskit_node_free(struct accesskit_node *node);
20022002

2003+
/**
2004+
* Caller must call `accesskit_string_free` with the return value.
2005+
*/
2006+
char *accesskit_node_debug(const struct accesskit_node *node);
2007+
20032008
struct accesskit_tree *accesskit_tree_new(accesskit_node_id root);
20042009

20052010
void accesskit_tree_free(struct accesskit_tree *tree);
@@ -2024,6 +2029,11 @@ void accesskit_tree_set_toolkit_version(struct accesskit_tree *tree,
20242029

20252030
void accesskit_tree_clear_toolkit_version(struct accesskit_tree *tree);
20262031

2032+
/**
2033+
* Caller must call `accesskit_string_free` with the return value.
2034+
*/
2035+
char *accesskit_tree_debug(const struct accesskit_tree *tree);
2036+
20272037
struct accesskit_tree_update *accesskit_tree_update_with_focus(
20282038
accesskit_node_id focus);
20292039

@@ -2048,6 +2058,12 @@ void accesskit_tree_update_clear_tree(struct accesskit_tree_update *update);
20482058
void accesskit_tree_update_set_focus(struct accesskit_tree_update *update,
20492059
accesskit_node_id focus);
20502060

2061+
/**
2062+
* Caller must call `accesskit_string_free` with the return value.
2063+
*/
2064+
char *accesskit_tree_update_debug(
2065+
const struct accesskit_tree_update *tree_update);
2066+
20512067
void accesskit_action_request_free(struct accesskit_action_request *request);
20522068

20532069
struct accesskit_affine accesskit_affine_identity(void);
@@ -2212,6 +2228,14 @@ void *accesskit_macos_adapter_hit_test(
22122228
void *activation_handler_userdata);
22132229
#endif
22142230

2231+
#if defined(__APPLE__)
2232+
/**
2233+
* Caller must call `accesskit_string_free` with the return value.
2234+
*/
2235+
char *accesskit_macos_adapter_debug(
2236+
const struct accesskit_macos_adapter *adapter);
2237+
#endif
2238+
22152239
#if defined(__APPLE__)
22162240
/**
22172241
* # Safety
@@ -2347,6 +2371,15 @@ void accesskit_unix_adapter_update_window_focus_state(
23472371
struct accesskit_unix_adapter *adapter, bool is_focused);
23482372
#endif
23492373

2374+
#if (defined(__linux__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
2375+
defined(__NetBSD__) || defined(__OpenBSD__))
2376+
/**
2377+
* Caller must call `accesskit_string_free` with the return value.
2378+
*/
2379+
char *accesskit_unix_adapter_debug(
2380+
const struct accesskit_unix_adapter *adapter);
2381+
#endif
2382+
23502383
#if defined(_WIN32)
23512384
/**
23522385
* Memory is also freed when calling this function.
@@ -2397,6 +2430,14 @@ struct accesskit_opt_lresult accesskit_windows_adapter_handle_wm_getobject(
23972430
void *activation_handler_userdata);
23982431
#endif
23992432

2433+
#if defined(_WIN32)
2434+
/**
2435+
* Caller must call `accesskit_string_free` with the return value.
2436+
*/
2437+
char *accesskit_windows_adapter_debug(
2438+
const struct accesskit_windows_adapter *adapter);
2439+
#endif
2440+
24002441
#if defined(_WIN32)
24012442
/**
24022443
* Creates a new Windows platform adapter using window subclassing.

src/common.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// the LICENSE-APACHE file) or the MIT license (found in
44
// the LICENSE-MIT file), at your option.
55

6-
use crate::{box_from_ptr, mut_from_ptr, opt_struct, ref_from_ptr, BoxCastPtr, CastPtr};
76
use accesskit::*;
87
use std::{
98
ffi::{CStr, CString},
@@ -12,6 +11,10 @@ use std::{
1211
ptr, slice,
1312
};
1413

14+
use crate::{
15+
box_from_ptr, debug_repr_from_ptr, mut_from_ptr, opt_struct, ref_from_ptr, BoxCastPtr, CastPtr,
16+
};
17+
1518
pub struct node {
1619
_private: [u8; 0],
1720
}
@@ -735,6 +738,12 @@ impl node {
735738
pub extern "C" fn accesskit_node_free(node: *mut node) {
736739
drop(box_from_ptr(node));
737740
}
741+
742+
/// Caller must call `accesskit_string_free` with the return value.
743+
#[no_mangle]
744+
pub extern "C" fn accesskit_node_debug(node: *const node) -> *mut c_char {
745+
debug_repr_from_ptr(node)
746+
}
738747
}
739748

740749
pub struct tree {
@@ -812,6 +821,12 @@ impl tree {
812821
let tree = mut_from_ptr(tree);
813822
tree.toolkit_version = None;
814823
}
824+
825+
/// Caller must call `accesskit_string_free` with the return value.
826+
#[no_mangle]
827+
pub extern "C" fn accesskit_tree_debug(tree: *const tree) -> *mut c_char {
828+
debug_repr_from_ptr(tree)
829+
}
815830
}
816831

817832
pub struct tree_update {
@@ -883,6 +898,12 @@ impl tree_update {
883898
let update = mut_from_ptr(update);
884899
update.focus = focus.into();
885900
}
901+
902+
/// Caller must call `accesskit_string_free` with the return value.
903+
#[no_mangle]
904+
pub extern "C" fn accesskit_tree_update_debug(tree_update: *const tree_update) -> *mut c_char {
905+
debug_repr_from_ptr(tree_update)
906+
}
886907
}
887908

888909
#[repr(C)]

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ mod unix;
3030
#[cfg(any(target_os = "windows", feature = "cbindgen"))]
3131
mod windows;
3232

33+
use std::{
34+
ffi::{c_char, CString},
35+
fmt::Debug,
36+
};
37+
3338
pub use common::*;
3439
pub use geometry::*;
3540
#[cfg(any(target_os = "macos", feature = "cbindgen"))]
@@ -176,3 +181,13 @@ macro_rules! opt_struct {
176181
}
177182
};
178183
}
184+
185+
pub(crate) fn debug_repr_from_ptr<F, T>(value: *const F) -> *mut c_char
186+
where
187+
F: CastConstPtr<RustType = T>,
188+
T: Debug,
189+
{
190+
let value = ref_from_ptr(value);
191+
let debug_repr = format!("{:?}", value);
192+
CString::new(debug_repr).unwrap().into_raw()
193+
}

src/macos.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
// the LICENSE-APACHE file) or the MIT license (found in
44
// the LICENSE-MIT file), at your option.
55

6-
use crate::{
7-
box_from_ptr, mut_from_ptr, tree_update_factory, tree_update_factory_userdata,
8-
ActionHandlerCallback, ActivationHandlerCallback, BoxCastPtr, CastPtr, FfiActionHandler,
9-
FfiActivationHandler,
10-
};
116
use accesskit_macos::{
127
add_focus_forwarder_to_window_class, Adapter, NSPoint, QueuedEvents, SubclassingAdapter,
138
};
14-
use std::{
15-
ffi::CStr,
16-
os::raw::{c_char, c_void},
9+
use std::ffi::{c_char, c_void, CStr};
10+
11+
use crate::{
12+
box_from_ptr, debug_repr_from_ptr, mut_from_ptr, tree_update_factory,
13+
tree_update_factory_userdata, ActionHandlerCallback, ActivationHandlerCallback, BoxCastPtr,
14+
CastPtr, FfiActionHandler, FfiActivationHandler,
1715
};
1816

1917
pub struct macos_queued_events {
@@ -134,6 +132,12 @@ impl macos_adapter {
134132
FfiActivationHandler::new(activation_handler, activation_handler_userdata);
135133
adapter.hit_test(NSPoint::new(x, y), &mut activation_handler) as *mut _
136134
}
135+
136+
/// Caller must call `accesskit_string_free` with the return value.
137+
#[no_mangle]
138+
pub extern "C" fn accesskit_macos_adapter_debug(adapter: *const macos_adapter) -> *mut c_char {
139+
debug_repr_from_ptr(adapter)
140+
}
137141
}
138142

139143
pub struct macos_subclassing_adapter {

src/unix.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
// the LICENSE-APACHE file) or the MIT license (found in
44
// the LICENSE-MIT file), at your option.
55

6-
use crate::{
7-
box_from_ptr, mut_from_ptr, tree_update_factory, tree_update_factory_userdata,
8-
ActionHandlerCallback, ActivationHandlerCallback, BoxCastPtr, CastPtr,
9-
DeactivationHandlerCallback, FfiActionHandler, FfiActivationHandler, FfiDeactivationHandler,
10-
};
116
use accesskit::Rect;
127
use accesskit_unix::Adapter;
13-
use std::os::raw::c_void;
8+
use std::ffi::{c_char, c_void};
9+
10+
use crate::{
11+
box_from_ptr, debug_repr_from_ptr, mut_from_ptr, tree_update_factory,
12+
tree_update_factory_userdata, ActionHandlerCallback, ActivationHandlerCallback, BoxCastPtr,
13+
CastPtr, DeactivationHandlerCallback, FfiActionHandler, FfiActivationHandler,
14+
FfiDeactivationHandler,
15+
};
1416

1517
pub struct unix_adapter {
1618
_private: [u8; 0],
@@ -85,4 +87,10 @@ impl unix_adapter {
8587
let adapter = mut_from_ptr(adapter);
8688
adapter.update_window_focus_state(is_focused);
8789
}
90+
91+
/// Caller must call `accesskit_string_free` with the return value.
92+
#[no_mangle]
93+
pub extern "C" fn accesskit_unix_adapter_debug(adapter: *const unix_adapter) -> *mut c_char {
94+
debug_repr_from_ptr(adapter)
95+
}
8896
}

src/windows.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
// the LICENSE-APACHE file) or the MIT license (found in
44
// the LICENSE-MIT file), at your option.
55

6+
use accesskit_windows::*;
7+
use std::ffi::{c_char, c_void};
8+
69
use crate::{
7-
box_from_ptr, mut_from_ptr, opt_struct, tree_update_factory, tree_update_factory_userdata,
8-
ActionHandlerCallback, ActivationHandlerCallback, BoxCastPtr, CastPtr, FfiActionHandler,
9-
FfiActivationHandler,
10+
box_from_ptr, debug_repr_from_ptr, mut_from_ptr, opt_struct, tree_update_factory,
11+
tree_update_factory_userdata, ActionHandlerCallback, ActivationHandlerCallback, BoxCastPtr,
12+
CastPtr, FfiActionHandler, FfiActivationHandler,
1013
};
11-
use accesskit_windows::*;
12-
use std::os::raw::c_void;
1314

1415
pub struct windows_queued_events {
1516
_private: [u8; 0],
@@ -102,6 +103,14 @@ impl windows_adapter {
102103
let lresult = adapter.handle_wm_getobject(wparam, lparam, &mut activation_handler);
103104
opt_lresult::from(lresult)
104105
}
106+
107+
/// Caller must call `accesskit_string_free` with the return value.
108+
#[no_mangle]
109+
pub extern "C" fn accesskit_windows_adapter_debug(
110+
adapter: *const windows_adapter,
111+
) -> *mut c_char {
112+
debug_repr_from_ptr(adapter)
113+
}
105114
}
106115

107116
pub struct windows_subclassing_adapter {

0 commit comments

Comments
 (0)