Skip to content

Commit

Permalink
Don't skip Option for nullable collections
Browse files Browse the repository at this point in the history
Nullable collection were represented as a `Vec<_>` or `[_]`, but an empty
collection is not the same as an undefined collection.

Fixes: gtk-rs#1133
See also: gtk-rs/gtk-rs-core#1257
  • Loading branch information
fengalin committed Dec 7, 2024
1 parent 15cb980 commit 44b11d0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/analysis/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ impl<'env> RustTypeBuilder<'env> {
let ok = |s: &str| Ok(RustType::from(s));
let ok_and_use = |s: &str| Ok(RustType::new_and_use(&s));
let err = |s: &str| Err(TypeError::Unimplemented(s.into()));
let mut skip_option = false;
let type_ = self.env.library.type_(self.type_id);
let mut rust_type = match *type_ {
Basic(fund) => {
Expand Down Expand Up @@ -355,7 +354,6 @@ impl<'env> RustTypeBuilder<'env> {
List(inner_tid) | SList(inner_tid) | CArray(inner_tid) | PtrArray(inner_tid)
if ConversionType::of(self.env, inner_tid) == ConversionType::Pointer =>
{
skip_option = true;
let inner_ref_mode = match self.env.type_(inner_tid) {
Class(..) | Interface(..) => RefMode::None,
Record(record) => match RecordType::of(record) {
Expand Down Expand Up @@ -409,7 +407,6 @@ impl<'env> RustTypeBuilder<'env> {
};

if let Some(s) = array_type {
skip_option = true;
if self.ref_mode.is_ref() {
Ok(format!("[{s}]").into())
} else {
Expand Down Expand Up @@ -604,7 +601,7 @@ impl<'env> RustTypeBuilder<'env> {
}
}

if *self.nullable && !skip_option {
if *self.nullable {
match ConversionType::of(self.env, self.type_id) {
ConversionType::Pointer | ConversionType::Scalar => {
rust_type = rust_type.map_any(|rust_type| {
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/function_body_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,9 @@ impl Builder {
} = trans.transformation_type
{
if let In = self.parameters[trans.ind_c] {
let value = Chunk::Custom(format!("{array_name}.len() as _"));
let value = Chunk::Custom(format!(
"{array_name}.as_ref().map(|a| a.len()).unwrap_or(0)"
));
chunks.push(Chunk::Let {
name: array_length_name.clone(),
is_mut: false,
Expand Down

0 comments on commit 44b11d0

Please sign in to comment.