Skip to content

Commit c918954

Browse files
committed
Prefer Option_ mappings for more types over transparent mappings
In general, the explicit `Option_` mappings are easier for downstream bindings to match against, and clearer for users anyway. Sadly, originally we were trying to avoid them where possible, so some types default to transparent mappings. Here we swap the default, at least for manually-mapped types, with a few explicit exceptions (that probably should be changed as well).
1 parent eee3524 commit c918954

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

c-bindings-gen/src/types.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,19 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
13811381
if let Some(resolved) = self.maybe_resolve_path(&p.path, generics) {
13821382
if self.c_type_has_inner_from_path(&resolved) { return true; }
13831383
if self.is_primitive(&resolved) { return false; }
1384-
if self.c_type_from_path(&resolved, false, false).is_some() { true } else { false }
1384+
// We want to move to using `Option_` mappings where possible rather than
1385+
// manual mappings, as it makes downstream bindings simpler and is more
1386+
// clear for users. Thus, we default to false but override for a few
1387+
// types which had mappings defined when we were avoiding the `Option_`s.
1388+
match &resolved as &str {
1389+
"lightning::ln::PaymentSecret" => true,
1390+
"lightning::ln::PaymentHash" => true,
1391+
"lightning::ln::PaymentPreimage" => true,
1392+
"lightning::ln::channelmanager::PaymentId" => true,
1393+
"bitcoin::hash_types::BlockHash" => true,
1394+
"secp256k1::PublicKey"|"bitcoin::secp256k1::PublicKey" => true,
1395+
_ => false,
1396+
}
13851397
} else { unimplemented!(); }
13861398
},
13871399
syn::Type::Tuple(_) => false,
@@ -1456,7 +1468,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
14561468
(".is_none() { core::ptr::null_mut() } else { ".to_owned(), format!("({}.unwrap())", var_access))
14571469
], " }", ContainerPrefixLocation::OutsideConv));
14581470
}
1459-
} else if self.is_primitive(&inner_path) || self.c_type_from_path(&inner_path, false, false).is_none() {
1471+
} else if !self.is_transparent_container("Option", is_ref, [single_contained.unwrap()].iter().map(|a| *a), generics) {
14601472
if self.is_primitive(&inner_path) || (!is_contained_ref && !is_ref) || only_contained_has_inner {
14611473
let inner_name = self.get_c_mangled_container_type(vec![single_contained.unwrap()], generics, "Option").unwrap();
14621474
return Some(("if ", vec![

0 commit comments

Comments
 (0)