Skip to content

Commit

Permalink
Merge pull request #925 from EPashkin/fix_builder_usage_version
Browse files Browse the repository at this point in the history
Add ImportsWithDefaults
  • Loading branch information
EPashkin committed Jun 7, 2020
2 parents 5b1a163 + 7bb6d0a commit e75bde2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/analysis/class_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn analyze_property(
if !for_builder {
return None;
}
imports.set_defaults(prop_version, &None);
let imports = &mut imports.with_defaults(prop_version, &None);
let type_str = used_rust_type(env, prop.typ, false);
if let Ok(ref s) = type_str {
if !s.contains("GString") {
Expand Down
4 changes: 1 addition & 3 deletions src/analysis/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ fn analyze_function(
.filter_map(|f| f.assertion)
.next();

imports.set_defaults(version, &cfg_condition);
let imports = &mut imports.with_defaults(version, &cfg_condition);

let ret = return_value::analyze(
env,
Expand Down Expand Up @@ -772,8 +772,6 @@ fn analyze_function(
let assertion =
assertion.unwrap_or_else(|| SafetyAssertionMode::of(env, is_method, &parameters));

imports.reset_defaults();

Info {
name,
glib_name: func.c_identifier.as_ref().unwrap().clone(),
Expand Down
50 changes: 44 additions & 6 deletions src/analysis/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::namespaces;
use crate::{library::Library, nameutil::crate_name, version::Version};
use std::cmp::Ordering;
use std::collections::btree_map::BTreeMap;
use std::ops::{Deref, DerefMut};
use std::vec::IntoIter;

fn is_first_char_up(s: &str) -> bool {
Expand Down Expand Up @@ -95,7 +96,12 @@ impl Imports {
}
}

pub fn set_defaults(&mut self, version: Option<Version>, constraint: &Option<String>) {
#[must_use = "ImportsWithDefault must live while defaults are needed"]
pub fn with_defaults(
&mut self,
version: Option<Version>,
constraint: &Option<String>,
) -> ImportsWithDefault<'_> {
let constraints = if let Some(constraint) = constraint {
vec![constraint.clone()]
} else {
Expand All @@ -105,9 +111,11 @@ impl Imports {
version,
constraints,
};

ImportsWithDefault::new(self)
}

pub fn reset_defaults(&mut self) {
fn reset_defaults(&mut self) {
self.defaults.clear();
}

Expand All @@ -128,7 +136,7 @@ impl Imports {
.entry(name.to_owned())
.or_insert_with(|| defaults.clone());
entry.update_version(self.defaults.version);
entry.update_constraints(self.defaults.constraints.clone());
entry.update_constraints(&self.defaults.constraints);
}
}

Expand Down Expand Up @@ -254,6 +262,35 @@ impl Imports {
}
}

pub struct ImportsWithDefault<'a> {
imports: &'a mut Imports,
}

impl<'a> ImportsWithDefault<'a> {
fn new(imports: &'a mut Imports) -> Self {
Self { imports }
}
}

impl Drop for ImportsWithDefault<'_> {
fn drop(&mut self) {
self.imports.reset_defaults()
}
}

impl Deref for ImportsWithDefault<'_> {
type Target = Imports;
fn deref(&self) -> &Self::Target {
self.imports
}
}

impl DerefMut for ImportsWithDefault<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.imports
}
}

#[derive(Clone, Debug, Default)]
pub struct ImportConditions {
pub version: Option<Version>,
Expand All @@ -262,7 +299,8 @@ pub struct ImportConditions {

impl ImportConditions {
fn clear(&mut self) {
*self = ImportConditions::default();
self.version = None;
self.constraints.clear();
}

fn update_version(&mut self, version: Option<Version>) {
Expand All @@ -284,7 +322,7 @@ impl ImportConditions {
}
}

fn update_constraints(&mut self, constraints: Vec<String>) {
fn update_constraints(&mut self, constraints: &[String]) {
// If the import is already present but doesn't have any constraint,
// we don't want to add one.
if self.constraints.is_empty() {
Expand All @@ -298,7 +336,7 @@ impl ImportConditions {
// Otherwise, we just check if the constraint
// is already present or not before adding it.
for constraint in constraints {
if !self.constraints.iter().any(|x| x == &constraint) {
if !self.constraints.iter().any(|x| x == constraint) {
self.constraints.push(constraint.clone());
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/analysis/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn analyze_property(
let generate_set = generate.is_some();
let generate = generate.unwrap_or_else(PropertyGenerateFlags::all);

imports.set_defaults(prop_version, &None);
let imports = &mut imports.with_defaults(prop_version, &None);

let type_string = rust_type(env, prop.typ);
let name_for_func = nameutil::signal_to_snake(&name);
Expand Down Expand Up @@ -313,8 +313,6 @@ fn analyze_property(
None
};

imports.reset_defaults();

(getter, setter, notify_signal)
}

Expand Down
3 changes: 1 addition & 2 deletions src/analysis/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn analyze_signal(
let deprecated_version = signal.deprecated_version;
let doc_hidden = configured_signals.iter().any(|f| f.doc_hidden);

imports.set_defaults(version, &None);
let imports = &mut imports.with_defaults(version, &None);

let connect_name = format!("connect_{}", nameutil::signal_to_snake(&signal.name));
let trampoline = trampolines::analyze(
Expand Down Expand Up @@ -110,7 +110,6 @@ fn analyze_signal(
imports.add("std::boxed::Box as Box_");
imports.add("glib_sys");
}
imports.reset_defaults();

let info = Info {
connect_name,
Expand Down

0 comments on commit e75bde2

Please sign in to comment.