libghostty_vt_sys/lib.rs
1#![allow(non_camel_case_types)]
2#![allow(non_snake_case)]
3#![allow(non_upper_case_globals)]
4#![allow(clippy::all)]
5#![allow(rustdoc::all)]
6
7mod bindings;
8
9use std::ops::Deref;
10
11pub use bindings::*;
12
13/// Initialize a "sized" FFI object.
14#[macro_export]
15macro_rules! sized {
16 ($ty:ty) => {{
17 let mut t = <$ty as ::std::default::Default>::default();
18 t.size = ::std::mem::size_of::<$ty>();
19 t
20 }};
21}
22
23impl<S> From<S> for bindings::String
24where
25 S: Deref<Target = str>,
26{
27 fn from(value: S) -> Self {
28 Self {
29 ptr: value.as_ptr(),
30 len: value.len(),
31 }
32 }
33}
34
35impl bindings::String {
36 /// Borrow the string as raw bytes. A null pointer is treated as an empty
37 /// string; the C API does not promise a non-null pointer for zero-length
38 /// strings, and `slice::from_raw_parts` requires one even at length zero.
39 ///
40 /// # Safety
41 ///
42 /// The caller must uphold that the associated lifetime is valid
43 /// with the given context behind the FFI string.
44 pub unsafe fn to_bytes<'a>(self) -> &'a [u8] {
45 if self.ptr.is_null() {
46 return &[];
47 }
48 // SAFETY: To be upheld by caller
49 unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
50 }
51
52 /// # Safety
53 ///
54 /// The caller must uphold that the associated lifetime is valid
55 /// with the given context behind the FFI string, and that it contains
56 /// valid UTF-8 data.
57 pub unsafe fn to_str<'a>(self) -> &'a str {
58 // SAFETY: To be upheld by caller
59 unsafe { std::str::from_utf8_unchecked(self.to_bytes()) }
60 }
61}