1#![deny(missing_debug_implementations)]
12#![deny(missing_docs)]
13#![cfg_attr(docsrs, feature(doc_cfg))]
14#![allow(clippy::missing_safety_doc)]
15
16use core::ffi::CStr;
21use core::ffi::{c_char, c_int};
22use core::net::SocketAddr;
23use core::str::FromStr;
24
25#[repr(i32)]
27#[derive(Debug, PartialEq, Eq, Copy, Clone)]
28pub enum RiceError {
29 Success = 0,
31 Failed = -1,
33 ResourceNotFound = -2,
35 AlreadyInProgress = -3,
37}
38
39#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
41pub struct RiceAddress(SocketAddr);
42
43impl RiceAddress {
44 pub fn new(addr: SocketAddr) -> Self {
46 Self(addr)
47 }
48
49 pub fn into_c_full(self) -> *const RiceAddress {
54 const_override(Box::into_raw(Box::new(self)))
55 }
56
57 pub unsafe fn into_rice_full(value: *const RiceAddress) -> Box<Self> {
59 unsafe { Box::from_raw(mut_override(value)) }
60 }
61
62 pub unsafe fn into_rice_none(value: *const RiceAddress) -> Self {
64 unsafe {
65 let boxed = Box::from_raw(mut_override(value));
66 let ret = *boxed;
67 core::mem::forget(boxed);
68 ret
69 }
70 }
71
72 pub fn inner(self) -> SocketAddr {
74 self.0
75 }
76}
77
78impl core::ops::Deref for RiceAddress {
79 type Target = SocketAddr;
80 fn deref(&self) -> &Self::Target {
81 &self.0
82 }
83}
84
85pub unsafe fn rice_address_new_from_string(string: *const c_char) -> *mut RiceAddress {
87 unsafe {
88 let Ok(string) = CStr::from_ptr(string).to_str() else {
89 return core::ptr::null_mut();
90 };
91 let Ok(saddr) = SocketAddr::from_str(string) else {
92 return core::ptr::null_mut();
93 };
94
95 mut_override(RiceAddress::into_c_full(RiceAddress::new(saddr)))
96 }
97}
98
99pub unsafe fn rice_address_cmp(addr: *const RiceAddress, other: *const RiceAddress) -> c_int {
101 unsafe {
102 match (addr.is_null(), other.is_null()) {
103 (true, true) => 0,
104 (true, false) => -1,
105 (false, true) => 1,
106 (false, false) => {
107 let addr = RiceAddress::into_rice_none(addr);
108 let other = RiceAddress::into_rice_none(other);
109 addr.cmp(&other) as c_int
110 }
111 }
112 }
113}
114
115pub unsafe fn rice_address_free(addr: *mut RiceAddress) {
117 unsafe {
118 if !addr.is_null() {
119 let _addr = Box::from_raw(addr);
120 }
121 }
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126#[repr(u32)]
127pub enum RiceTransportType {
128 Udp,
130 Tcp,
132}
133
134fn mut_override<T>(val: *const T) -> *mut T {
135 val as *mut T
136}
137
138fn const_override<T>(val: *mut T) -> *const T {
139 val as *const T
140}