Skip to content

Commit

Permalink
Merge pull request #895 from sdroege/borrowed
Browse files Browse the repository at this point in the history
Generate code for new from_glib_borrow signature
  • Loading branch information
EPashkin committed Apr 5, 2020
2 parents f42ad44 + 466b0c8 commit 997cbcb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
25 changes: 20 additions & 5 deletions src/codegen/function_body_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,17 @@ impl Builder {
add_chunk_for_type(env, par.typ, par, &mut body, &ty_name, nullable);
if ty_name == "GString" {
if *nullable {
arguments.push(Chunk::Name(format!("{}.map(|x| x.as_str())", par.name)));
arguments.push(Chunk::Name(format!(
"{}.as_ref().map(|x| x.as_str())",
par.name
)));
} else {
arguments.push(Chunk::Name(format!("{}.as_str()", par.name)));
}
continue;
}
if *nullable && !is_fundamental {
arguments.push(Chunk::Name(format!("{}.as_ref()", par.name)));
arguments.push(Chunk::Name(format!("{}.as_ref().as_ref()", par.name)));
continue;
}
arguments.push(Chunk::Name(format!(
Expand Down Expand Up @@ -1340,12 +1343,24 @@ fn add_chunk_for_type(
let type_name;
if ty_name == "GString" {
if *nullable {
type_name = String::from(": Option<GString>");
if par.conversion_type == ConversionType::Borrow {
type_name = String::from(": Borrowed<Option<GString>>");
} else {
type_name = String::from(": Option<GString>");
}
} else {
type_name = String::from(": GString");
if par.conversion_type == ConversionType::Borrow {
type_name = String::from(": Borrowed<GString>");
} else {
type_name = String::from(": GString");
}
}
} else if par.transfer == library::Transfer::None && *nullable {
type_name = format!(": Option<{}>", ty_name);
if par.conversion_type == ConversionType::Borrow {
type_name = format!(": Borrowed<Option<{}>>", ty_name);
} else {
type_name = format!(": Option<{}>", ty_name);
}
} else {
type_name = String::from("");
}
Expand Down
13 changes: 9 additions & 4 deletions src/codegen/trampoline_from_glib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ impl TrampolineFromGlib for Transformation {
left = format!("{}::{}", type_name, left);
}
}
if need_downcast {
right = format!("{}.unsafe_cast()", right);
}

if !nullable || !is_borrow {
if !nullable {
left = format!("&{}", left);
} else if nullable && is_borrow {
right = format!("{}.as_ref().as_ref()", right);
} else {
right = format!("{}.as_ref()", right);
}

if need_downcast && is_borrow {
right = format!("{}.unsafe_cast_ref()", right);
} else if need_downcast {
right = format!("{}.unsafe_cast()", right);
}

format!("{}{}{}", left, self.name, right)
}
Unknown => format!("/*Unknown conversion*/{}", self.name),
Expand Down

0 comments on commit 997cbcb

Please sign in to comment.