1#![deny(missing_debug_implementations)]
10#![deny(missing_docs)]
11#![allow(clippy::missing_safety_doc)]
12
13use core::ffi::CStr;
18use core::ffi::{c_char, c_int};
19use core::net::SocketAddr;
20use core::str::FromStr;
21
22#[repr(i32)]
24#[derive(Debug, PartialEq, Eq, Copy, Clone)]
25pub enum RiceError {
26 Success = 0,
28 Failed = -1,
30 ResourceNotFound = -2,
32 AlreadyInProgress = -3,
34}
35
36#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
38pub struct RiceAddress(SocketAddr);
39
40impl RiceAddress {
41 pub fn new(addr: SocketAddr) -> Self {
43 Self(addr)
44 }
45
46 pub fn into_c_full(self) -> *const RiceAddress {
51 const_override(Box::into_raw(Box::new(self)))
52 }
53
54 pub unsafe fn into_rice_full(value: *const RiceAddress) -> Box<Self> {
56 Box::from_raw(mut_override(value))
57 }
58
59 pub unsafe fn into_rice_none(value: *const RiceAddress) -> Self {
61 let boxed = Box::from_raw(mut_override(value));
62 let ret = *boxed;
63 core::mem::forget(boxed);
64 ret
65 }
66
67 pub fn inner(self) -> SocketAddr {
69 self.0
70 }
71}
72
73impl core::ops::Deref for RiceAddress {
74 type Target = SocketAddr;
75 fn deref(&self) -> &Self::Target {
76 &self.0
77 }
78}
79
80pub unsafe fn rice_address_new_from_string(string: *const c_char) -> *mut RiceAddress {
82 let Ok(string) = CStr::from_ptr(string).to_str() else {
83 return core::ptr::null_mut();
84 };
85 let Ok(saddr) = SocketAddr::from_str(string) else {
86 return core::ptr::null_mut();
87 };
88
89 mut_override(RiceAddress::into_c_full(RiceAddress::new(saddr)))
90}
91
92pub unsafe fn rice_address_cmp(addr: *const RiceAddress, other: *const RiceAddress) -> c_int {
94 match (addr.is_null(), other.is_null()) {
95 (true, true) => 0,
96 (true, false) => -1,
97 (false, true) => 1,
98 (false, false) => {
99 let addr = RiceAddress::into_rice_none(addr);
100 let other = RiceAddress::into_rice_none(other);
101 addr.cmp(&other) as c_int
102 }
103 }
104}
105
106pub unsafe fn rice_address_free(addr: *mut RiceAddress) {
108 if !addr.is_null() {
109 let _addr = Box::from_raw(addr);
110 }
111}
112
113#[derive(Debug, Clone, Copy, PartialEq, Eq)]
115#[repr(u32)]
116pub enum RiceTransportType {
117 Udp,
119 Tcp,
121}
122
123fn mut_override<T>(val: *const T) -> *mut T {
124 val as *mut T
125}
126
127fn const_override<T>(val: *mut T) -> *const T {
128 val as *const T
129}