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

codegen/object: Check for generate_safety_asserts when generating builders #1608

Merged
merged 2 commits into from
Oct 22, 2024
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
11 changes: 11 additions & 0 deletions src/analysis/safety_assertion_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ pub enum SafetyAssertionMode {
InMainThread,
}

impl std::fmt::Display for SafetyAssertionMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SafetyAssertionMode::None => f.write_str(""),
SafetyAssertionMode::NotInitialized => f.write_str("assert_not_initialized!();"),
SafetyAssertionMode::Skip => f.write_str("skip_assert_initialized!();"),
SafetyAssertionMode::InMainThread => f.write_str("assert_initialized_main_thread!();"),
}
}
}

impl FromStr for SafetyAssertionMode {
type Err = String;
fn from_str(name: &str) -> Result<SafetyAssertionMode, String> {
Expand Down
8 changes: 2 additions & 6 deletions src/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
library::{self, TypeId},
nameutil::use_glib_type,
version::Version,
writer::{primitives::tabs, safety_assertion_mode_to_str, ToCode},
writer::{primitives::tabs, ToCode},
};

// We follow the rules of the `return_self_not_must_use` clippy lint:
Expand Down Expand Up @@ -421,11 +421,7 @@ pub fn body_chunk_futures(
writeln!(body)?;

if !async_future.assertion.is_none() {
writeln!(
body,
"{}",
safety_assertion_mode_to_str(async_future.assertion)
)?;
writeln!(body, "{}", async_future.assertion)?;
}
let skip = usize::from(async_future.is_method);

Expand Down
10 changes: 4 additions & 6 deletions src/codegen/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::{
library::{self, Nullable},
nameutil,
traits::IntoString,
writer::safety_assertion_mode_to_str,
};

pub fn generate(w: &mut dyn Write, env: &Env, analysis: &analysis::object::Info) -> Result<()> {
Expand Down Expand Up @@ -457,11 +456,10 @@ fn generate_builder(w: &mut dyn Write, env: &Env, analysis: &analysis::object::I
pub fn build(self) -> {name} {{",
name = analysis.name,
)?;
writeln!(
w,
"{}",
safety_assertion_mode_to_str(SafetyAssertionMode::InMainThread)
)?;

if env.config.generate_safety_asserts {
writeln!(w, "{}", SafetyAssertionMode::InMainThread)?;
}

// The split allows us to not have clippy::let_and_return lint disabled
if let Some(code) = analysis.builder_postprocess.as_ref() {
Expand Down
10 changes: 0 additions & 10 deletions src/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,3 @@ pub mod to_code; // TODO:remove pub
pub mod untabber;

pub use self::{defines::TAB, to_code::ToCode};
use crate::analysis::safety_assertion_mode::SafetyAssertionMode;

pub fn safety_assertion_mode_to_str(s: SafetyAssertionMode) -> &'static str {
match s {
SafetyAssertionMode::None => "",
SafetyAssertionMode::NotInitialized => "assert_not_initialized!();",
SafetyAssertionMode::Skip => "skip_assert_initialized!();",
SafetyAssertionMode::InMainThread => "assert_initialized_main_thread!();",
}
}
4 changes: 2 additions & 2 deletions src/writer/to_code.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Write;

use super::{primitives::*, safety_assertion_mode_to_str};
use super::primitives::*;
use crate::{
chunk::{Chunk, Param, TupleMode},
codegen::{translate_from_glib::TranslateFromGlib, translate_to_glib::TranslateToGlib},
Expand Down Expand Up @@ -137,7 +137,7 @@ impl ToCode for Chunk {
lines.push(s);
lines
}
AssertInit(x) => vec![safety_assertion_mode_to_str(x).to_owned()],
AssertInit(x) => vec![x.to_string()],
Connect {
ref signal,
ref trampoline,
Expand Down
Loading