Skip to content

Commit 349213a

Browse files
committed
Support passing lifetime'd enums to C
While this is generally unsafe, we have to do something for `CandidateRouteHop`, which is passed to the scorer. Because its incredibly performance-sensitive, forcing the bindings users to take a performance hit just to make the scoring interface marginally safer seems like a bad trade-off.
1 parent c14513f commit 349213a

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

c-bindings-gen/src/blocks.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,14 @@ pub fn write_method_call_params<W: std::io::Write>(w: &mut W, sig: &syn::Signatu
748748
/// Prints concrete generic parameters for a struct/trait/function, including the less-than and
749749
/// greater-than symbols, if any generic parameters are defined.
750750
pub fn maybe_write_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver, concrete_lifetimes: bool) {
751+
maybe_write_generics_intern(w, generics, generics_impld, types, concrete_lifetimes, false);
752+
}
753+
754+
pub fn maybe_write_non_lifetime_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver) {
755+
maybe_write_generics_intern(w, generics, generics_impld, types, false, true);
756+
}
757+
758+
fn maybe_write_generics_intern<W: std::io::Write>(w: &mut W, generics: &syn::Generics, generics_impld: &syn::PathArguments, types: &TypeResolver, concrete_lifetimes: bool, dummy_lifetimes: bool) {
751759
let mut gen_types = GenericTypes::new(None);
752760
assert!(gen_types.learn_generics(generics, types));
753761
if generics.params.is_empty() { return; }
@@ -789,7 +797,9 @@ pub fn maybe_write_generics<W: std::io::Write>(w: &mut W, generics: &syn::Generi
789797
}
790798
},
791799
syn::GenericParam::Lifetime(lt) => {
792-
if concrete_lifetimes {
800+
if dummy_lifetimes {
801+
write!(w, "'_").unwrap();
802+
} else if concrete_lifetimes {
793803
write!(w, "'static").unwrap();
794804
} else {
795805
write!(w, "{}'{}", if idx != 0 { ", " } else { "" }, lt.lifetime.ident).unwrap();

c-bindings-gen/src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,7 +1809,11 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
18091809

18101810
macro_rules! write_conv {
18111811
($fn_sig: expr, $to_c: expr, $ref: expr) => {
1812-
writeln!(w, "\t#[allow(unused)]\n\tpub(crate) fn {} {{\n\t\tmatch {} {{", $fn_sig, if $to_c { "native" } else { "self" }).unwrap();
1812+
writeln!(w, "\t#[allow(unused)]\n\tpub(crate) fn {} {{", $fn_sig).unwrap();
1813+
if $to_c && $ref {
1814+
writeln!(w, "\t\tlet native = unsafe {{ &*(native as *const _ as *const c_void as *const native{}) }};", e.ident).unwrap();
1815+
}
1816+
writeln!(w, "\t\tmatch {} {{", if $to_c { "native" } else { "self" }).unwrap();
18131817
for var in e.variants.iter() {
18141818
write!(w, "\t\t\t{}{}::{} ", if $to_c { "native" } else { "" }, e.ident, var.ident).unwrap();
18151819
let mut empty_tuple_variant = false;
@@ -1933,7 +1937,10 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type
19331937
}
19341938
write_conv!(format!("into_native(self) -> native{}", e.ident), false, false);
19351939
if is_clonable {
1936-
write_conv!(format!("from_native(native: &native{}) -> Self", e.ident), true, true);
1940+
let mut args = Vec::new();
1941+
maybe_write_non_lifetime_generics(&mut args, &e.generics, &syn::PathArguments::None, &types);
1942+
let fn_line = format!("from_native(native: &{}Import{}) -> Self", e.ident, String::from_utf8(args).unwrap());
1943+
write_conv!(fn_line, true, true);
19371944
}
19381945
write_conv!(format!("native_into(native: native{}) -> Self", e.ident), true, false);
19391946
writeln!(w, "}}").unwrap();

0 commit comments

Comments
 (0)