Skip to content

Commit

Permalink
cleanup: Use Display for SafetyAssertionMode
Browse files Browse the repository at this point in the history
Instead of that weird function
  • Loading branch information
bilelmoussaoui committed Oct 22, 2024
1 parent 18c5428 commit a172dda
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
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
7 changes: 1 addition & 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 @@ -459,11 +458,7 @@ fn generate_builder(w: &mut dyn Write, env: &Env, analysis: &analysis::object::I
)?;

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

// The split allows us to not have clippy::let_and_return lint disabled
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

0 comments on commit a172dda

Please sign in to comment.