Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.18 backports #1525

Merged
merged 8 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/analysis/mod.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading