Skip to content

Commit

Permalink
Merge pull request #1525 from sdroege/0.18-backports
Browse files Browse the repository at this point in the history
0.18 backports
  • Loading branch information
sdroege committed Nov 2, 2023
2 parents c7aa72a + d5db1ed commit 23d7c10
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/analysis/mod.rs
Expand Up @@ -349,7 +349,7 @@ fn analyze(env: &mut Env, tid: TypeId, deps: &[TypeId]) {
}
}

fn is_all_deps_analyzed(env: &mut Env, deps: &[TypeId]) -> bool {
fn is_all_deps_analyzed(env: &Env, deps: &[TypeId]) -> bool {
for tid in deps {
let full_name = tid.full_name(&env.library);
if !env.analysis.objects.contains_key(&full_name) {
Expand Down
5 changes: 3 additions & 2 deletions src/codegen/alias.rs
Expand Up @@ -5,7 +5,7 @@ use std::{

use crate::{
analysis::{namespaces, rust_type::RustType},
codegen::general,
codegen::general::{doc_alias, start_comments},
config::gobjects::GObject,
env::Env,
file_saver,
Expand Down Expand Up @@ -36,7 +36,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {

let path = root_path.join("alias.rs");
file_saver::save_to_file(path, env.config.make_backup, |w| {
general::start_comments(w, &env.config)?;
start_comments(w, &env.config)?;
writeln!(w)?;
writeln!(w, "#[allow(unused_imports)]")?;
writeln!(w, "use crate::auto::*;")?;
Expand All @@ -56,6 +56,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {

fn generate_alias(env: &Env, w: &mut dyn Write, alias: &Alias, _: &GObject) -> Result<()> {
let typ = RustType::try_new(env, alias.typ).into_string();
doc_alias(w, &alias.c_identifier, "", 0)?;
writeln!(w, "pub type {} = {};", alias.name, typ)?;

Ok(())
Expand Down
18 changes: 11 additions & 7 deletions src/codegen/enums.rs
Expand Up @@ -450,13 +450,17 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
writeln!(
w,
"impl StaticType for {name} {{
#[inline]
fn static_type() -> {glib_type} {{
unsafe {{ from_glib({sys_crate_name}::{get_type}()) }}
}}
}}",
sys_crate_name = sys_crate_name,
#[inline]",
name = enum_.name,
)?;
doc_alias(w, get_type, "", 1)?;
writeln!(
w,
" fn static_type() -> {glib_type} {{
unsafe {{ from_glib({sys_crate_name}::{get_type}()) }}
}}
}}",
sys_crate_name = sys_crate_name,
get_type = get_type,
glib_type = use_glib_type(env, "Type")
)?;
Expand All @@ -473,7 +477,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
type BuilderFn = fn(&str, Self) -> {param_spec_builder}<Self>;
fn param_spec_builder() -> Self::BuilderFn {{
|name, default_value| Self::ParamSpec::builder_with_default(name, default_value)
Self::ParamSpec::builder_with_default
}}
}}",
name = enum_.name,
Expand Down
18 changes: 11 additions & 7 deletions src/codegen/flags.rs
Expand Up @@ -274,13 +274,17 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
writeln!(
w,
"impl StaticType for {name} {{
#[inline]
fn static_type() -> {glib_type} {{
unsafe {{ from_glib({sys_crate_name}::{get_type}()) }}
}}
}}",
sys_crate_name = sys_crate_name,
#[inline]",
name = flags.name,
)?;
doc_alias(w, get_type, "", 1)?;
writeln!(
w,
" fn static_type() -> {glib_type} {{
unsafe {{ from_glib({sys_crate_name}::{get_type}()) }}
}}
}}",
sys_crate_name = sys_crate_name,
get_type = get_type,
glib_type = use_glib_type(env, "Type")
)?;
Expand All @@ -297,7 +301,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
type BuilderFn = fn(&str) -> {param_spec_builder}<Self>;
fn param_spec_builder() -> Self::BuilderFn {{
|name| Self::ParamSpec::builder(name)
Self::ParamSpec::builder
}}
}}",
name = flags.name,
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/function_body_chunk.rs
Expand Up @@ -685,7 +685,7 @@ impl Builder {
trampoline: &AsyncTrampoline,
) {
chunks.push(Chunk::Custom(String::from(
r###"
r#"
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
Expand All @@ -695,7 +695,7 @@ impl Builder {
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
"###,
"#,
)));

chunks.push(Chunk::Let {
Expand Down
45 changes: 30 additions & 15 deletions src/codegen/general.rs
@@ -1,6 +1,6 @@
use std::{
collections::BTreeMap,
fmt::Display,
fmt::{Display, Write as FmtWrite},
io::{Result, Write},
ops::Index,
};
Expand Down Expand Up @@ -35,20 +35,27 @@ pub fn start_comments(w: &mut dyn Write, conf: &Config) -> Result<()> {
pub fn start_comments_no_version(w: &mut dyn Write, conf: &Config) -> Result<()> {
writeln!(
w,
"// This file was generated by gir (https://github.com/gtk-rs/gir)
{}// DO NOT EDIT",
"// This file was generated by gir (https://github.com/gtk-rs/gir)"
)?;
write!(
w,
"{}",
conf.girs_version
.iter()
.map(|info| {
format!(
"// from {}{}\n",
.fold(String::new(), |mut output, info| {
writeln!(
output,
"// from {}{}",
info.gir_dir.display(),
info.get_repository_url()
.map_or_else(String::new, |url| format!(" ({url})")),
)
.unwrap();
output
})
.collect::<String>()
)
)?;
writeln!(w, "// DO NOT EDIT")?;
Ok(())
}

pub fn single_version_file(w: &mut dyn Write, conf: &Config, prefix: &str) -> Result<()> {
Expand All @@ -60,22 +67,30 @@ pub fn single_version_file(w: &mut dyn Write, conf: &Config, prefix: &str) -> Re
VERSION,
conf.girs_version
.iter()
.map(|info| {
.fold(String::new(), |mut output, info| {
match (info.get_repository_url(), info.get_hash()) {
(Some(url), Some(hash)) => format!(
"{}from {} ({} @ {})\n",
(Some(url), Some(hash)) => writeln!(
output,
"{}from {} ({} @ {})",
prefix,
info.gir_dir.display(),
url,
hash,
),
(None, Some(hash)) => {
format!("{}from {} (@ {})\n", prefix, info.gir_dir.display(), hash,)
writeln!(
output,
"{}from {} (@ {})",
prefix,
info.gir_dir.display(),
hash,
)
}
_ => format!("{}from {}\n", prefix, info.gir_dir.display()),
_ => writeln!(output, "{}from {}", prefix, info.gir_dir.display()),
}
})
.collect::<String>(),
.unwrap();
output
}),
)
}

Expand Down
8 changes: 4 additions & 4 deletions src/codegen/sys/build.rs
Expand Up @@ -39,8 +39,8 @@ fn generate_build_script(w: &mut dyn Write, env: &Env, split_build_rs: bool) ->
writeln!(
w,
"{}",
r##"#[cfg(not(docsrs))]
use std::process;"##
r#"#[cfg(not(docsrs))]
use std::process;"#
)?;

if split_build_rs {
Expand All @@ -51,7 +51,7 @@ use std::process;"##
write!(
w,
"{}",
r##"
r#"
#[cfg(docsrs)]
fn main() {} // prevent linking libraries to avoid documentation failure
Expand All @@ -62,7 +62,7 @@ fn main() {
process::exit(1);
}
}
"##
"#
)
}

Expand Down
16 changes: 8 additions & 8 deletions src/codegen/sys/tests.rs
Expand Up @@ -252,7 +252,7 @@ fn generate_constant_c(
writeln!(
w,
"{}",
r####"
r#"
#define PRINT_CONSTANT(CONSTANT_NAME) \
printf("%s;", #CONSTANT_NAME); \
printf(_Generic((CONSTANT_NAME), \
Expand All @@ -274,7 +274,7 @@ fn generate_constant_c(
long double: "%ld"), \
CONSTANT_NAME); \
printf("\n");
"####
"#
)?;

writeln!(w, "{}", r"int main() {")?;
Expand Down Expand Up @@ -325,7 +325,7 @@ fn generate_abi_rs(
writeln!(
w,
"{}",
r####"
r#"
#[derive(Clone, Debug)]
struct Compiler {
pub args: Vec<String>,
Expand Down Expand Up @@ -511,7 +511,7 @@ fn get_c_output(name: &str) -> Result<String, Box<dyn Error>> {
Ok(String::from_utf8(output.stdout)?)
}
const RUST_LAYOUTS: &[(&str, Layout)] = &["####
const RUST_LAYOUTS: &[(&str, Layout)] = &["#
)?;
for ctype in ctypes {
general::cfg_condition(w, ctype.cfg_condition.as_ref(), false, 1)?;
Expand All @@ -521,9 +521,9 @@ const RUST_LAYOUTS: &[(&str, Layout)] = &["####
writeln!(
w,
"{}",
r##"];
r#"];
const RUST_CONSTANTS: &[(&str, &str)] = &["##
const RUST_CONSTANTS: &[(&str, &str)] = &["#
)?;
for cconst in cconsts {
writeln!(
Expand All @@ -536,8 +536,8 @@ const RUST_CONSTANTS: &[(&str, &str)] = &["##
writeln!(
w,
"{}",
r##"];
r#"];
"##
"#
)
}
2 changes: 1 addition & 1 deletion src/codegen/trait_impls.rs
Expand Up @@ -259,7 +259,7 @@ fn generate_ord(
impl PartialOrd for {type_name} {{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {{
{call}.partial_cmp(&0)
Some(self.cmp(other))
}}
}}
Expand Down
2 changes: 1 addition & 1 deletion src/library.rs
Expand Up @@ -609,7 +609,7 @@ macro_rules! impl_lexical_ord {

impl PartialOrd for $name {
fn partial_cmp(&self, other: &$name) -> Option<Ordering> {
self.$field.partial_cmp(&other.$field)
Some(self.cmp(other))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Expand Up @@ -89,7 +89,7 @@ fn build_config() -> Result<RunKind, String> {
matches.opt_str("c").as_str_ref(),
work_mode,
&matches.opt_strs("d"),
matches.free.get(0).as_str_ref(),
matches.free.first().as_str_ref(),
matches.free.get(1).as_str_ref(),
matches.opt_str("o").as_str_ref(),
matches.opt_str("doc-target-path").as_str_ref(),
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Expand Up @@ -1479,7 +1479,7 @@ impl Library {

fn read_object_c_type<'a>(
&mut self,
parser: &mut XmlParser<'_>,
parser: &XmlParser<'_>,
elem: &'a Element,
) -> Result<&'a str, String> {
elem.attr("type")
Expand Down

0 comments on commit 23d7c10

Please sign in to comment.