Skip to main content

rice_c/
ffi.rs

1// Copyright (C) 2025 Matthew Waters <matthew@centricular.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8//
9// SPDX-License-Identifier: MIT OR Apache-2.0
10
11//! FFI module for the raw `rice-proto` C API.
12
13#![allow(non_camel_case_types)]
14#![allow(non_upper_case_globals)]
15#![allow(unused)]
16#![allow(missing_debug_implementations)]
17#![allow(missing_docs)]
18
19use crate::mut_override;
20
21#[cfg(docsrs)]
22include!("bindings.rs");
23#[cfg(not(docsrs))]
24include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
25
26impl Default for RiceStreamIncomingData {
27    fn default() -> Self {
28        Self {
29            handled: false,
30            have_more_data: false,
31            data: RiceDataImpl {
32                ptr: core::ptr::null_mut(),
33                size: 0,
34            },
35        }
36    }
37}
38
39impl RiceDataImpl {
40    pub(crate) fn to_c(slice: &[u8]) -> Self {
41        Self {
42            ptr: mut_override(slice.as_ptr()),
43            size: slice.len(),
44        }
45    }
46}
47
48impl RiceData {
49    pub(crate) fn to_c_owned(slice: &[u8]) -> Self {
50        RiceData {
51            tag: RICE_DATA_OWNED,
52            field1: RiceData__bindgen_ty_1 {
53                field2: core::mem::ManuallyDrop::new(RiceData__bindgen_ty_1__bindgen_ty_2 {
54                    owned: RiceDataImpl::to_c(slice),
55                }),
56            },
57        }
58    }
59}
60
61impl RiceGatheredCandidate {
62    pub(crate) unsafe fn zeroed() -> Self {
63        unsafe {
64            RiceGatheredCandidate {
65                candidate: RiceCandidate::zeroed(),
66                turn_agent: core::ptr::null_mut(),
67            }
68        }
69    }
70}
71
72impl RiceCandidate {
73    pub(crate) unsafe fn zeroed() -> Self {
74        RiceCandidate {
75            component_id: 1,
76            candidate_type: RICE_CANDIDATE_TYPE_HOST,
77            transport_type: RICE_TRANSPORT_TYPE_UDP,
78            foundation: core::ptr::null_mut(),
79            priority: 0,
80            address: core::ptr::null(),
81            base_address: core::ptr::null(),
82            related_address: core::ptr::null(),
83            tcp_type: RICE_TCP_TYPE_NONE,
84            extensions: core::ptr::null_mut(),
85            extensions_len: 0,
86        }
87    }
88}
89
90impl Clone for RiceCandidate {
91    fn clone(&self) -> Self {
92        unsafe {
93            let mut ret = RiceCandidate::zeroed();
94            crate::ffi::rice_candidate_copy_into(self, &mut ret);
95            ret
96        }
97    }
98}
99
100impl core::fmt::Debug for RiceAddress {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        let addr = crate::Address::from_c_none(self);
103        write!(f, "{addr:?}")
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn rice_candidate_clone_zeroed() {
113        let _log = crate::tests::test_init_log();
114
115        unsafe {
116            let zeroed = RiceCandidate::zeroed();
117            let _cloned = zeroed.clone();
118        }
119    }
120}