diff --git a/crates/pmacct-gauze-lib/src/capi/bgp/update.rs b/crates/pmacct-gauze-lib/src/capi/bgp/update.rs index 442def2..045c296 100644 --- a/crates/pmacct-gauze-lib/src/capi/bgp/update.rs +++ b/crates/pmacct-gauze-lib/src/capi/bgp/update.rs @@ -1,14 +1,14 @@ use crate::capi::bgp::{reconcile_as24path, DebugUpdateType, WrongBgpMessageTypeError}; use crate::capi::bmp::{BmpMessageValueOpaque, WrongBmpMessageTypeError}; use crate::cresult::CResult; +use crate::cslice::CSlice; +use crate::cslice::RustFree; use crate::extensions::bgp_attribute::ExtendBgpAttribute; use crate::extensions::community::{ExtendExtendedCommunity, ExtendLargeCommunity}; use crate::extensions::mp_reach::ExtendMpReach; use crate::extensions::next_hop::ExtendLabeledNextHop; use crate::free_cslice_t; use crate::log::{pmacct_log, LogPriority}; -use crate::slice::CSlice; -use crate::slice::RustFree; use netgauze_bgp_pkt::nlri::MplsLabel; use netgauze_bgp_pkt::path_attribute::{ Aigp, As4Path, AsPath, MpReach, MpUnreach, PathAttributeValue, diff --git a/crates/pmacct-gauze-lib/src/capi/bmp/mod.rs b/crates/pmacct-gauze-lib/src/capi/bmp/mod.rs index 8ec7428..466eabf 100644 --- a/crates/pmacct-gauze-lib/src/capi/bmp/mod.rs +++ b/crates/pmacct-gauze-lib/src/capi/bmp/mod.rs @@ -1,9 +1,9 @@ use crate::cresult::CResult; +use crate::cslice::CSlice; +pub use crate::cslice::RustFree; use crate::extensions::bmp_message::ExtendBmpPeerHeader; use crate::extensions::information_tlv::TlvExtension; use crate::free_cslice_t; -use crate::slice::CSlice; -pub use crate::slice::RustFree; use libc::{AF_INET, AF_INET6}; use netgauze_bmp_pkt::iana::BmpMessageType; use netgauze_bmp_pkt::{BmpMessageValue, InitiationInformation, TerminationInformation}; diff --git a/crates/pmacct-gauze-lib/src/capi/bmp/stats.rs b/crates/pmacct-gauze-lib/src/capi/bmp/stats.rs index 20815e6..4f84a7e 100644 --- a/crates/pmacct-gauze-lib/src/capi/bmp/stats.rs +++ b/crates/pmacct-gauze-lib/src/capi/bmp/stats.rs @@ -1,10 +1,10 @@ use crate::capi::bmp::{BmpMessageValueOpaque, WrongBmpMessageTypeError}; use crate::cresult::CResult; +use crate::cslice::CSlice; +use crate::cslice::RustFree; use crate::extensions::bmp_statistics::ExtendBmpStatistics; use crate::free_cslice_t; use crate::log::{pmacct_log, LogPriority}; -use crate::slice::CSlice; -use crate::slice::RustFree; use netgauze_bmp_pkt::BmpMessageValue; use pmacct_gauze_bindings::bmp_log_stats; diff --git a/crates/pmacct-gauze-lib/src/coption.rs b/crates/pmacct-gauze-lib/src/coption.rs index 236f466..bd264cc 100644 --- a/crates/pmacct-gauze-lib/src/coption.rs +++ b/crates/pmacct-gauze-lib/src/coption.rs @@ -1,5 +1,6 @@ +/// Re-implementation of [Option] but FFI compatible #[repr(C)] -#[derive(Debug)] +#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)] pub enum COption { None, Some(T), diff --git a/crates/pmacct-gauze-lib/src/cresult.rs b/crates/pmacct-gauze-lib/src/cresult.rs index c385e35..275e600 100644 --- a/crates/pmacct-gauze-lib/src/cresult.rs +++ b/crates/pmacct-gauze-lib/src/cresult.rs @@ -1,8 +1,9 @@ use std::convert::Infallible; use std::ops::{ControlFlow, FromResidual, Try}; +/// Re-implementation of [Result] but FFI compatible #[repr(C)] -#[derive(Debug)] +#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)] pub enum CResult { Ok(S), Err(E), @@ -21,7 +22,6 @@ impl Try for CResult { /// [Self::Ok] type Output = Self; - /// Result type Residual = Result; fn from_output(output: Self::Output) -> Self { diff --git a/crates/pmacct-gauze-lib/src/slice.rs b/crates/pmacct-gauze-lib/src/cslice.rs similarity index 100% rename from crates/pmacct-gauze-lib/src/slice.rs rename to crates/pmacct-gauze-lib/src/cslice.rs diff --git a/crates/pmacct-gauze-lib/src/lib.rs b/crates/pmacct-gauze-lib/src/lib.rs index 227ed1d..bafb170 100644 --- a/crates/pmacct-gauze-lib/src/lib.rs +++ b/crates/pmacct-gauze-lib/src/lib.rs @@ -7,21 +7,26 @@ // TODO add testing that validates return values using C functions // TODO derive macro for automatic c function print for structs/enums implementation +/// The actual methods exposed to C #[cfg(feature = "capi")] pub mod capi; -#[cfg(feature = "capi")] + +/// Representation of a [Option] but FFI-compatible pub mod coption; -#[cfg(feature = "capi")] + +/// Representation of a [Result] but FFI-compatible pub mod cresult; -#[cfg(feature = "capi")] + +/// Representation of a slice in C. Allows converting from/to [Vec] +pub mod cslice; + +/// Extension traits with helper functions for NetGauze types pub mod extensions; -#[cfg(feature = "capi")] -pub mod slice; #[macro_use] pub mod macros; -#[cfg(feature = "capi")] +/// Module handling pmacct-compatible logging from pmacct-gauze pub mod log; #[cfg(test)] diff --git a/crates/pmacct-gauze-lib/src/macros.rs b/crates/pmacct-gauze-lib/src/macros.rs index 41bd1a5..2c0cdb0 100644 --- a/crates/pmacct-gauze-lib/src/macros.rs +++ b/crates/pmacct-gauze-lib/src/macros.rs @@ -1,19 +1,19 @@ pub use paste; -/// Generate a function called `CSlice_free_T` for C to free a [crate::slice::CSlice] for type `T`. +/// Generate a function called `CSlice_free_T` for C to free a [crate::cslice::CSlice] for type `T`. /// -/// This variant of the macro automatically implements [crate::slice::RustFree] for the type `T` -/// The automatic implementation for [crate::slice::RustFree::rust_free] on `T` just drops the value +/// This variant of the macro automatically implements [crate::cslice::RustFree] for the type `T` +/// The automatic implementation for [crate::cslice::RustFree::rust_free] on `T` just drops the value /// without specific behaviour. /// -/// If you want to customize the [crate::slice::RustFree::rust_free] implementation, +/// If you want to customize the [crate::cslice::RustFree::rust_free] implementation, /// look into [free_cslice_t_with_item_free] /// /// If `T` has generic parameters, the macro can't use T to name the function automatically. /// You will need to provide the function suffix as a 2nd parameter of the macro. /// ```rust /// use pmacct_gauze_lib::free_cslice_t; -/// use pmacct_gauze_lib::slice::*; +/// use pmacct_gauze_lib::cslice::*; /// struct SomeGenericStruct(T); /// /* free_cslice_t!(SomeGenericStruct); this can't work because /// * CSlice_free_SomeGenericStruct is not a valid function name @@ -34,7 +34,7 @@ macro_rules! free_cslice_t { } #[automatically_derived] - impl $crate::slice::RustFree for $typ { + impl $crate::cslice::RustFree for $typ { fn rust_free(self) {} } }; @@ -46,17 +46,17 @@ pub use free_cslice_t; // TODO qol: derive macro with automatic rust_free impl -/// Generate a function called `CSlice_free_T` for C to free a [crate::slice::CSlice] for type `T` +/// Generate a function called `CSlice_free_T` for C to free a [crate::cslice::CSlice] for type `T` /// /// This macro work exactly as the macro [free_cslice_t] works -/// without implementing [crate::slice::RustFree] automatically for `T`. +/// without implementing [crate::cslice::RustFree] automatically for `T`. /// -/// This allows you to implement [crate::slice::RustFree] for `T` if you need a special behaviour to free the type. +/// This allows you to implement [crate::cslice::RustFree] for `T` if you need a special behaviour to free the type. /// /// Example: /// ``` /// use pmacct_gauze_lib::free_cslice_t_with_item_free; -/// use pmacct_gauze_lib::slice::RustFree; +/// use pmacct_gauze_lib::cslice::RustFree; /// struct Struct; /// /// // This type is needed to be able to implement RustFree as we can't impl a foreign trait on arbitrary types