Skip to content

Commit

Permalink
rename slice to cslice, add module doc
Browse files Browse the repository at this point in the history
  • Loading branch information
mxyns committed Feb 27, 2024
1 parent ff3ef12 commit dc0352c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 25 deletions.
4 changes: 2 additions & 2 deletions crates/pmacct-gauze-lib/src/capi/bgp/update.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/pmacct-gauze-lib/src/capi/bmp/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
4 changes: 2 additions & 2 deletions crates/pmacct-gauze-lib/src/capi/bmp/stats.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
3 changes: 2 additions & 1 deletion crates/pmacct-gauze-lib/src/coption.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
None,
Some(T),
Expand Down
4 changes: 2 additions & 2 deletions crates/pmacct-gauze-lib/src/cresult.rs
Original file line number Diff line number Diff line change
@@ -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<S, E> {
Ok(S),
Err(E),
Expand All @@ -21,7 +22,6 @@ impl<T, E> Try for CResult<T, E> {
/// [Self::Ok]
type Output = Self;

/// Result<Infallible, ParseError>
type Residual = Result<Infallible, E>;

fn from_output(output: Self::Output) -> Self {
Expand Down
File renamed without changes.
17 changes: 11 additions & 6 deletions crates/pmacct-gauze-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
20 changes: 10 additions & 10 deletions crates/pmacct-gauze-lib/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
pub use paste;

/// Generate a function called `CSlice_free_T` for C to free a [crate::slice::CSlice<T>] for type `T`.
/// Generate a function called `CSlice_free_T` for C to free a [crate::cslice::CSlice<T>] 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>(T);
/// /* free_cslice_t!(SomeGenericStruct<u16>); this can't work because
/// * CSlice_free_SomeGenericStruct<T> is not a valid function name
Expand All @@ -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) {}
}
};
Expand All @@ -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<T>] for type `T`
/// Generate a function called `CSlice_free_T` for C to free a [crate::cslice::CSlice<T>] 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
Expand Down

0 comments on commit dc0352c

Please sign in to comment.