From 1ef3cbed33817a09e5b3543bf3b2b8cfc8febbe8 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sun, 6 Dec 2020 13:30:10 +0100 Subject: [PATCH] FIXUP: Move to_string update_func tasks into is_stringify --- src/analysis/special_functions.rs | 51 +++++++++++++------------------ 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/src/analysis/special_functions.rs b/src/analysis/special_functions.rs index deee8bdde..6a1239544 100644 --- a/src/analysis/special_functions.rs +++ b/src/analysis/special_functions.rs @@ -62,7 +62,7 @@ pub type Infos = BTreeMap; /// Returns true on functions that take an instance as single argument and /// return a string as result. -fn is_stringify(func: &FuncInfo) -> bool { +fn is_stringify(func: &mut FuncInfo, parent_type: &LibType, obj: &GObject) -> bool { if func.parameters.c_parameters.len() != 1 { return false; } @@ -70,43 +70,34 @@ fn is_stringify(func: &FuncInfo) -> bool { return false; } - func.ret - .parameter - .as_ref() - .map_or(false, |p| p.typ == TypeId::tid_utf8()) -} - -fn update_func(func: &mut FuncInfo, type_: Type, parent_type: &LibType, obj: &GObject) -> bool { - if func.visibility != Visibility::Comment { - func.visibility = visibility(type_); - } + if let Some(ret) = func.ret.parameter.as_mut() { + if ret.typ != TypeId::tid_utf8() { + return false; + } - if matches!(type_, Type::Display | Type::Stringify) { if func.name == "to_string" { // Rename to to_str to make sure it doesn't clash with ToString::to_string func.name = "to_str".to_owned(); // As to not change old code behaviour, assume non-nullability outside - // enums and flags only. Function inside enums and flags have been - // appropriately marked in Gir. + // enums and flags only, and exclusively for to_string. Function inside + // enums and flags have been appropriately marked in Gir. if !obj.trust_return_value_nullability && !matches!(parent_type, LibType::Enumeration(_) | LibType::Bitfield(_)) { - if let Some(par) = func.ret.parameter.as_mut() { - *par.nullable = false; - } + *ret.nullable = false; } } - // Cannot generate Display implementation for Option<> - if func - .ret - .parameter - .as_ref() - .map_or(false, |ret| *ret.nullable) - { - return false; - } + !*ret.nullable + } else { + false + } +} + +fn update_func(func: &mut FuncInfo, type_: Type) -> bool { + if func.visibility != Visibility::Comment { + func.visibility = visibility(type_); } true } @@ -121,9 +112,9 @@ pub fn extract(functions: &mut Vec, parent_type: &LibType, obj: &GObje let type_ = Type::extract(&func.name); let type_ = match type_ { - Some(Type::Display) if is_stringify(func) => type_, + Some(Type::Display) if is_stringify(func, parent_type, obj) => type_, Some(Type::Display) => None, - None if is_stringify(func) => Some(Type::Stringify), + None if is_stringify(func, parent_type, obj) => Some(Type::Stringify), t => t, }; @@ -132,7 +123,7 @@ pub fn extract(functions: &mut Vec, parent_type: &LibType, obj: &GObje destroy = Some((func.glib_name.clone(), pos)); continue; } - if !update_func(func, type_, parent_type, obj) { + if !update_func(func, type_) { continue; } if func.name == "copy" { @@ -170,7 +161,7 @@ pub fn extract(functions: &mut Vec, parent_type: &LibType, obj: &GObje if let Some((glib_name, pos)) = destroy { let ty_ = Type::from_str("destroy").unwrap(); let func = &mut functions[pos]; - update_func(func, ty_, parent_type, obj); + update_func(func, ty_); specials.insert( ty_, Info {