Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap VCL_BOOL in a struct to ensure type safety #52

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ fn main() {
.derive_debug(true)
.derive_default(true)
.generate_cstr(true)
//
// VCL_ACL = *const vrt_acl
// VCL_BACKEND = *const director
// VCL_BLOB = *const vrt_blob
// VCL_BODY = *const ::std::os::raw::c_void
// VCL_BOOL = ::std::os::raw::c_uint
.new_type_alias_deref("VCL_BOOL")
// VCL_BYTES = i64
// VCL_DURATION = vtim_dur
// VCL_ENUM = *const ::std::os::raw::c_char
// VCL_HEADER = *const gethdr_s
// VCL_HTTP = *mut http
// VCL_INSTANCE = ::std::os::raw::c_void
// VCL_INT = i64
// VCL_IP = *const suckaddr
// VCL_PROBE = *const vrt_backend_probe
// VCL_REAL = f64
// VCL_REGEX = *const vre
// VCL_STEVEDORE = *const stevedore
// VCL_STRANDS = *const strands
// VCL_STRING = *const ::std::os::raw::c_char
// VCL_SUB = *const vcl_sub
// VCL_TIME = vtim_real
// VCL_VCL = *mut vcl
// VCL_VOID = ::std::os::raw::c_void
//
.generate()
.expect("Unable to generate bindings");

Expand Down
17 changes: 16 additions & 1 deletion src/bindings.rs.saved
Original file line number Diff line number Diff line change
Expand Up @@ -4295,7 +4295,22 @@ pub type VCL_ACL = *const vrt_acl;
pub type VCL_BACKEND = *const director;
pub type VCL_BLOB = *const vrt_blob;
pub type VCL_BODY = *const ::std::ffi::c_void;
pub type VCL_BOOL = ::std::ffi::c_uint;
#[repr(transparent)]
#[derive(Debug, Default, Copy, Clone)]
pub struct VCL_BOOL(pub ::std::ffi::c_uint);
impl ::std::ops::Deref for VCL_BOOL {
type Target = ::std::ffi::c_uint;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl ::std::ops::DerefMut for VCL_BOOL {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub type VCL_BYTES = i64;
pub type VCL_DURATION = vtim_dur;
pub type VCL_ENUM = *const ::std::ffi::c_char;
Expand Down
67 changes: 48 additions & 19 deletions src/vcl/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ vcl_types! {
VCL_BACKEND,
VCL_BLOB,
VCL_BODY,
VCL_BOOL,
// VCL_BOOL, // need?
VCL_BYTES,
VCL_DURATION,
// VCL_ENUM, // same as VCL_BODY
Expand All @@ -124,12 +124,6 @@ impl IntoVCL<()> for () {
}
}

impl IntoVCL<VCL_BOOL> for bool {
fn into_vcl(self, _: &mut WS) -> Result<VCL_BOOL, String> {
Ok(self as VCL_BOOL)
}
}

impl IntoVCL<VCL_DURATION> for Duration {
fn into_vcl(self, _: &mut WS) -> Result<VCL_DURATION, String> {
Ok(self.as_secs_f64())
Expand Down Expand Up @@ -193,9 +187,11 @@ impl<'a> IntoVCL<VCL_PROBE> for COWProbe<'a> {
}
probe.timeout = self.timeout.into_vcl(ws)?;
probe.interval = self.interval.into_vcl(ws)?;
probe.exp_status = self.exp_status.into_vcl(ws)?;
probe.window = self.window.into_vcl(ws)?;
probe.initial = self.initial.into_vcl(ws)?;

// FIXME: these were auto-type-casted via VCL_BOOL(?)
probe.exp_status = self.exp_status;
probe.window = self.window;
probe.initial = self.initial;
Ok(probe)
}
}
Expand All @@ -218,9 +214,9 @@ impl IntoVCL<VCL_PROBE> for Probe {
}
probe.timeout = self.timeout.into_vcl(ws)?;
probe.interval = self.interval.into_vcl(ws)?;
probe.exp_status = self.exp_status.into_vcl(ws)?;
probe.window = self.window.into_vcl(ws)?;
probe.initial = self.initial.into_vcl(ws)?;
probe.exp_status = self.exp_status;
probe.window = self.window;
probe.initial = self.initial;
Ok(probe)
}
}
Expand Down Expand Up @@ -426,12 +422,6 @@ impl IntoRust<i64> for VCL_INT {
}
}

impl IntoRust<bool> for VCL_BOOL {
fn into_rust(self) -> bool {
self != 0
}
}

impl<'a> IntoRust<Cow<'a, str>> for VCL_STRING {
fn into_rust(self) -> Cow<'a, str> {
let s = if self.is_null() { EMPTY_STRING } else { self };
Expand Down Expand Up @@ -525,3 +515,42 @@ impl IntoRust<Option<SocketAddr>> for VCL_IP {
}
}
}

macro_rules! impl_type_cast {
($ident:ident, $typ:ty) => {
impl From<$typ> for $ident {
fn from(b: $typ) -> Self {
Self(b.into())
}
}
impl IntoVCL<$ident> for $typ {
fn into_vcl(self, _: &mut WS) -> Result<$ident, String> {
Ok(self.into())
}
}
impl IntoRust<$typ> for $ident {
fn into_rust(self) -> $typ {
self.into()
}
}
impl IntoResult<$typ> for $ident {
type Item = Self;
fn into_result(self) -> Result<Self::Item, String> {
Ok(self)
}
}
impl VCLDefault for $ident {
type Item = Self;
fn vcl_default() -> Self::Item {
<$typ>::default().into()
}
}
};
}

impl_type_cast!(VCL_BOOL, bool);
impl From<VCL_BOOL> for bool {
fn from(b: VCL_BOOL) -> Self {
b.0 != 0
}
}