tesseract_swift_transport/
protocol.rs1use std::mem::ManuallyDrop;
2
3use tesseract_one::Protocol;
4use crate::utils::{string::CString, ptr::SyncPtr, Void};
5
6#[repr(C)]
7pub struct TesseractProtocol(SyncPtr<Void>);
8
9impl TesseractProtocol {
10 pub fn new<P: Protocol + 'static>(protocol: P) -> Self {
11 Self::new_dyn(Box::new(protocol))
12 }
13
14 pub fn new_dyn(protocol: Box<dyn Protocol>) -> Self {
15 Self(SyncPtr::new(protocol).as_void())
16 }
17}
18
19impl Protocol for TesseractProtocol {
20 fn id(&self) -> String {
21 unsafe { self.0.as_typed_ref::<Box<dyn Protocol>>().unwrap().id().into() }
22 }
23}
24
25impl Drop for TesseractProtocol {
26 fn drop(&mut self) {
27 let _ = unsafe { self.0.take_typed::<Box<dyn Protocol>>() };
28 }
29}
30
31#[no_mangle]
32pub unsafe extern "C" fn tesseract_protocol_is_equal(lhs: &TesseractProtocol, rhs: &TesseractProtocol) -> bool {
33 lhs.id() == rhs.id()
34}
35
36#[no_mangle]
37pub unsafe extern "C" fn tesseract_protocol_get_id(protocol: &TesseractProtocol) -> ManuallyDrop<CString> {
38 ManuallyDrop::new(protocol.id().into())
39}
40
41#[no_mangle]
42pub unsafe extern "C" fn tesseract_protocol_free(protocol: &mut ManuallyDrop<TesseractProtocol>) {
43 let _ = ManuallyDrop::take(protocol);
44}