#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(clippy::missing_safety_doc)]
use core::ffi::CStr;
use core::ffi::{c_char, c_int};
use core::net::SocketAddr;
use core::str::FromStr;
#[repr(i32)]
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum RiceError {
Success = 0,
Failed = -1,
ResourceNotFound = -2,
AlreadyInProgress = -3,
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub struct RiceAddress(SocketAddr);
impl RiceAddress {
pub fn new(addr: SocketAddr) -> Self {
Self(addr)
}
pub fn into_c_full(self) -> *const RiceAddress {
const_override(Box::into_raw(Box::new(self)))
}
pub unsafe fn into_rice_full(value: *const RiceAddress) -> Box<Self> {
unsafe { Box::from_raw(mut_override(value)) }
}
pub unsafe fn into_rice_none(value: *const RiceAddress) -> Self {
unsafe {
let boxed = Box::from_raw(mut_override(value));
let ret = *boxed;
core::mem::forget(boxed);
ret
}
}
pub fn inner(self) -> SocketAddr {
self.0
}
}
impl core::ops::Deref for RiceAddress {
type Target = SocketAddr;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub unsafe fn rice_address_new_from_string(string: *const c_char) -> *mut RiceAddress {
unsafe {
let Ok(string) = CStr::from_ptr(string).to_str() else {
return core::ptr::null_mut();
};
let Ok(saddr) = SocketAddr::from_str(string) else {
return core::ptr::null_mut();
};
mut_override(RiceAddress::into_c_full(RiceAddress::new(saddr)))
}
}
pub unsafe fn rice_address_cmp(addr: *const RiceAddress, other: *const RiceAddress) -> c_int {
unsafe {
match (addr.is_null(), other.is_null()) {
(true, true) => 0,
(true, false) => -1,
(false, true) => 1,
(false, false) => {
let addr = RiceAddress::into_rice_none(addr);
let other = RiceAddress::into_rice_none(other);
addr.cmp(&other) as c_int
}
}
}
}
pub unsafe fn rice_address_free(addr: *mut RiceAddress) {
unsafe {
if !addr.is_null() {
let _addr = Box::from_raw(addr);
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum RiceTransportType {
Udp,
Tcp,
}
fn mut_override<T>(val: *const T) -> *mut T {
val as *mut T
}
fn const_override<T>(val: *mut T) -> *const T {
val as *const T
}