Skip to content

Commit 93f71af

Browse files
committed
[bindings] Include a GenericTypes context in more places
A few places got a None in the previous commit to avoid increasing the diff size. However, it makes sense to have GenericTypes contexts there, so we pipe them through the neccessary places.
1 parent 7e60313 commit 93f71af

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

c-bindings-gen/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
628628
if let syn::ReturnType::Type(_, rtype) = &$m.sig.output {
629629
if let syn::Type::Reference(r) = &**rtype {
630630
write!(w, "\n\t\t{}{}: ", $indent, $m.sig.ident).unwrap();
631-
types.write_empty_rust_val(w, &*r.elem);
631+
types.write_empty_rust_val(Some(&gen_types), w, &*r.elem);
632632
writeln!(w, ",\n{}\t\tset_{}: Some({}_{}_set_{}),", $indent, $m.sig.ident, ident, trait_obj.ident, $m.sig.ident).unwrap();
633633
printed = true;
634634
}
@@ -722,7 +722,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
722722
writeln!(w, "\t// This is a bit race-y in the general case, but for our specific use-cases today, we're safe").unwrap();
723723
writeln!(w, "\t// Specifically, we must ensure that the first time we're called it can never be in parallel").unwrap();
724724
write!(w, "\tif ").unwrap();
725-
types.write_empty_rust_val_check(w, &*r.elem, &format!("trait_self_arg.{}", $m.sig.ident));
725+
types.write_empty_rust_val_check(Some(&gen_types), w, &*r.elem, &format!("trait_self_arg.{}", $m.sig.ident));
726726
writeln!(w, " {{").unwrap();
727727
writeln!(w, "\t\tunsafe {{ &mut *(trait_self_arg as *const {} as *mut {}) }}.{} = {}_{}_{}(trait_self_arg.this_arg);", trait_obj.ident, trait_obj.ident, $m.sig.ident, ident, trait_obj.ident, $m.sig.ident).unwrap();
728728
writeln!(w, "\t}}").unwrap();

c-bindings-gen/src/types.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
681681
fn is_known_container(&self, full_path: &str, is_ref: bool) -> bool {
682682
(full_path == "Result" && !is_ref) || (full_path == "Vec" && !is_ref) || full_path.ends_with("Tuple")
683683
}
684-
fn to_c_conversion_container_new_var<'b>(&self, full_path: &str, is_ref: bool, single_contained: Option<&syn::Type>, var_name: &syn::Ident, var_access: &str)
684+
fn to_c_conversion_container_new_var<'b>(&self, generics: Option<&GenericTypes>, full_path: &str, is_ref: bool, single_contained: Option<&syn::Type>, var_name: &syn::Ident, var_access: &str)
685685
// Returns prefix + Vec<(prefix, var-name-to-inline-convert)> + suffix
686686
// expecting one element in the vec per generic type, each of which is inline-converted
687687
-> Option<(&'b str, Vec<(String, String)>, &'b str)> {
@@ -700,7 +700,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
700700
},
701701
"Option" => {
702702
if let Some(syn::Type::Path(p)) = single_contained {
703-
if self.c_type_has_inner_from_path(&self.resolve_path(&p.path, None)) {
703+
if self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics)) {
704704
if is_ref {
705705
return Some(("if ", vec![
706706
(".is_none() { std::ptr::null() } else { ".to_owned(), format!("({}.as_ref().unwrap())", var_access))
@@ -714,7 +714,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
714714
}
715715
if let Some(t) = single_contained {
716716
let mut v = Vec::new();
717-
self.write_empty_rust_val(&mut v, t);
717+
self.write_empty_rust_val(generics, &mut v, t);
718718
let s = String::from_utf8(v).unwrap();
719719
return Some(("if ", vec![
720720
(format!(".is_none() {{ {} }} else {{ ", s), format!("({}.unwrap())", var_access))
@@ -727,7 +727,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
727727

728728
/// only_contained_has_inner implies that there is only one contained element in the container
729729
/// and it has an inner field (ie is an "opaque" type we've defined).
730-
fn from_c_conversion_container_new_var<'b>(&self, full_path: &str, is_ref: bool, single_contained: Option<&syn::Type>, var_name: &syn::Ident, var_access: &str)
730+
fn from_c_conversion_container_new_var<'b>(&self, generics: Option<&GenericTypes>, full_path: &str, is_ref: bool, single_contained: Option<&syn::Type>, var_name: &syn::Ident, var_access: &str)
731731
// Returns prefix + Vec<(prefix, var-name-to-inline-convert)> + suffix
732732
// expecting one element in the vec per generic type, each of which is inline-converted
733733
-> Option<(&'b str, Vec<(String, String)>, &'b str)> {
@@ -746,7 +746,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
746746
},
747747
"Option" => {
748748
if let Some(syn::Type::Path(p)) = single_contained {
749-
if self.c_type_has_inner_from_path(&self.resolve_path(&p.path, None)) {
749+
if self.c_type_has_inner_from_path(&self.resolve_path(&p.path, generics)) {
750750
if is_ref {
751751
return Some(("if ", vec![(".inner.is_null() { None } else { Some((*".to_string(), format!("{}", var_name))], ").clone()) }"))
752752
} else {
@@ -757,7 +757,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
757757

758758
if let Some(t) = single_contained {
759759
let mut v = Vec::new();
760-
let needs_deref = self.write_empty_rust_val_check_suffix(&mut v, t);
760+
let needs_deref = self.write_empty_rust_val_check_suffix(generics, &mut v, t);
761761
let s = String::from_utf8(v).unwrap();
762762
if needs_deref {
763763
return Some(("if ", vec![
@@ -1023,10 +1023,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
10231023

10241024
/// Prints a constructor for something which is "uninitialized" (but obviously not actually
10251025
/// unint'd memory).
1026-
pub fn write_empty_rust_val<W: std::io::Write>(&self, w: &mut W, t: &syn::Type) {
1026+
pub fn write_empty_rust_val<W: std::io::Write>(&self, generics: Option<&GenericTypes>, w: &mut W, t: &syn::Type) {
10271027
match t {
10281028
syn::Type::Path(p) => {
1029-
let resolved = self.resolve_path(&p.path, None);
1029+
let resolved = self.resolve_path(&p.path, generics);
10301030
if self.crate_types.opaques.get(&resolved).is_some() {
10311031
write!(w, "crate::{} {{ inner: std::ptr::null_mut(), is_owned: true }}", resolved).unwrap();
10321032
} else {
@@ -1056,10 +1056,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
10561056
/// Prints a suffix to determine if a variable is empty (ie was set by write_empty_rust_val),
10571057
/// returning whether we need to dereference the inner value before using it (ie it is a
10581058
/// pointer).
1059-
pub fn write_empty_rust_val_check_suffix<W: std::io::Write>(&self, w: &mut W, t: &syn::Type) -> bool {
1059+
pub fn write_empty_rust_val_check_suffix<W: std::io::Write>(&self, generics: Option<&GenericTypes>, w: &mut W, t: &syn::Type) -> bool {
10601060
match t {
10611061
syn::Type::Path(p) => {
1062-
let resolved = self.resolve_path(&p.path, None);
1062+
let resolved = self.resolve_path(&p.path, generics);
10631063
if self.crate_types.opaques.get(&resolved).is_some() {
10641064
write!(w, ".inner.is_null()").unwrap();
10651065
false
@@ -1092,11 +1092,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
10921092
}
10931093

10941094
/// Prints a suffix to determine if a variable is empty (ie was set by write_empty_rust_val).
1095-
pub fn write_empty_rust_val_check<W: std::io::Write>(&self, w: &mut W, t: &syn::Type, var_access: &str) {
1095+
pub fn write_empty_rust_val_check<W: std::io::Write>(&self, generics: Option<&GenericTypes>, w: &mut W, t: &syn::Type, var_access: &str) {
10961096
match t {
10971097
syn::Type::Path(_) => {
10981098
write!(w, "{}", var_access).unwrap();
1099-
self.write_empty_rust_val_check_suffix(w, t);
1099+
self.write_empty_rust_val_check_suffix(generics, w, t);
11001100
},
11011101
syn::Type::Array(a) => {
11021102
if let syn::Expr::Lit(l) = &a.len {
@@ -1108,7 +1108,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
11081108
self.from_c_conversion_prefix_from_path(&arrty, false).unwrap(),
11091109
var_access,
11101110
self.from_c_conversion_suffix_from_path(&arrty, false).unwrap()).unwrap();
1111-
self.write_empty_rust_val_check_suffix(w, t);
1111+
self.write_empty_rust_val_check_suffix(generics, w, t);
11121112
} else { unimplemented!(); }
11131113
} else { unimplemented!(); }
11141114
}
@@ -1561,7 +1561,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
15611561
pub fn write_to_c_conversion_new_var_inner<W: std::io::Write>(&self, w: &mut W, ident: &syn::Ident, var_access: &str, t: &syn::Type, generics: Option<&GenericTypes>, ptr_for_ref: bool) -> bool {
15621562
self.write_conversion_new_var_intern(w, ident, var_access, t, generics, false, ptr_for_ref, true,
15631563
&|a, b| self.to_c_conversion_new_var_from_path(a, b),
1564-
&|a, b, c, d, e| self.to_c_conversion_container_new_var(a, b, c, d, e),
1564+
&|a, b, c, d, e| self.to_c_conversion_container_new_var(generics, a, b, c, d, e),
15651565
// We force ptr_for_ref here since we can't generate a ref on one line and use it later
15661566
&|a, b, c, d, e, f| self.write_to_c_conversion_inline_prefix_inner(a, b, c, d, e, f),
15671567
&|a, b, c, d, e, f| self.write_to_c_conversion_inline_suffix_inner(a, b, c, d, e, f))
@@ -1572,7 +1572,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
15721572
pub fn write_from_c_conversion_new_var<W: std::io::Write>(&self, w: &mut W, ident: &syn::Ident, t: &syn::Type, generics: Option<&GenericTypes>) -> bool {
15731573
self.write_conversion_new_var_intern(w, ident, &format!("{}", ident), t, generics, false, false, false,
15741574
&|a, b| self.from_c_conversion_new_var_from_path(a, b),
1575-
&|a, b, c, d, e| self.from_c_conversion_container_new_var(a, b, c, d, e),
1575+
&|a, b, c, d, e| self.from_c_conversion_container_new_var(generics, a, b, c, d, e),
15761576
// We force ptr_for_ref here since we can't generate a ref on one line and use it later
15771577
&|a, b, c, d, e, _f| self.write_from_c_conversion_prefix_inner(a, b, c, d, e),
15781578
&|a, b, c, d, e, _f| self.write_from_c_conversion_suffix_inner(a, b, c, d, e))

0 commit comments

Comments
 (0)