From ec81047ac19d0a657d3ca5bcd47941df3f0460e5 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Thu, 1 Nov 2018 00:52:48 +0100 Subject: [PATCH] Use pattern matching instead of if and else that return true and false Differential Revision: https://phabricator.kde.org/D16576 --- src/configuration.rs | 60 +++++++++--------------------------- src/configuration_private.rs | 2 +- src/cpp.rs | 16 +++++----- src/rust.rs | 2 ++ 4 files changed, 24 insertions(+), 56 deletions(-) diff --git a/src/configuration.rs b/src/configuration.rs index b4e370f..1ac9bc7 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -229,36 +229,14 @@ impl SimpleTypePrivate for SimpleType { match self { SimpleType::QString => "const QString&", SimpleType::QByteArray => "const QByteArray&", - SimpleType::Bool => "bool", - SimpleType::Float => "float", - SimpleType::Double => "double", - SimpleType::Void => "void", - SimpleType::Qint8 => "qint8", - SimpleType::Qint16 => "qint16", - SimpleType::Qint32 => "qint32", - SimpleType::Qint64 => "qint64", - SimpleType::QUint8 => "quint8", - SimpleType::QUint16 => "quint16", - SimpleType::QUint32 => "quint32", - SimpleType::QUint64 => "quint64", + _ => self.name(), } } fn c_set_type(&self) -> &str { match self { SimpleType::QString => "qstring_t", SimpleType::QByteArray => "qbytearray_t", - SimpleType::Bool => "bool", - SimpleType::Float => "float", - SimpleType::Double => "double", - SimpleType::Void => "void", - SimpleType::Qint8 => "qint8", - SimpleType::Qint16 => "qint16", - SimpleType::Qint32 => "qint32", - SimpleType::Qint64 => "qint64", - SimpleType::QUint8 => "quint8", - SimpleType::QUint16 => "quint16", - SimpleType::QUint32 => "quint32", - SimpleType::QUint64 => "quint64", + _ => self.name(), } } fn rust_type(&self) -> &str { @@ -284,21 +262,13 @@ impl SimpleTypePrivate for SimpleType { SimpleType::QString => "String::new()", SimpleType::QByteArray => "Vec::new()", SimpleType::Bool => "false", - SimpleType::Float => "0.0", - SimpleType::Double => "0.0", + SimpleType::Float | SimpleType::Double => "0.0", SimpleType::Void => "()", - SimpleType::Qint8 => "0", - SimpleType::Qint16 => "0", - SimpleType::Qint32 => "0", - SimpleType::Qint64 => "0", - SimpleType::QUint8 => "0", - SimpleType::QUint16 => "0", - SimpleType::QUint32 => "0", - SimpleType::QUint64 => "0", + _ => "0", } } - fn is_complex(self) -> bool { - self == SimpleType::QString || self == SimpleType::QByteArray + fn is_complex(&self) -> bool { + self == &SimpleType::QString || self == &SimpleType::QByteArray } } @@ -310,17 +280,15 @@ pub enum Type { impl TypePrivate for Type { fn is_object(&self) -> bool { - if let Type::Object(_) = self { - true - } else { - false + match self { + Type::Object(_) => true, + _ => false, } } fn is_complex(&self) -> bool { - if let Type::Simple(simple) = self { - simple.is_complex() - } else { - false + match self { + Type::Simple(simple) => simple.is_complex(), + _ => false, } } fn name(&self) -> &str { @@ -381,9 +349,9 @@ impl ItemPropertyPrivate for ItemProperty { self.item_property_type.is_complex() } fn cpp_set_type(&self) -> String { - let mut t = self.item_property_type.cpp_set_type().to_string(); + let t = self.item_property_type.cpp_set_type().to_string(); if self.optional { - t = "option_".to_string() + &t; + return "option_".to_string() + &t; } t } diff --git a/src/configuration_private.rs b/src/configuration_private.rs index 0834902..da6b422 100644 --- a/src/configuration_private.rs +++ b/src/configuration_private.rs @@ -37,7 +37,7 @@ pub trait SimpleTypePrivate { fn c_set_type(&self) -> &str; fn rust_type(&self) -> &str; fn rust_type_init(&self) -> &str; - fn is_complex(self) -> bool; + fn is_complex(&self) -> bool; } pub trait ItemPropertyPrivate { diff --git a/src/cpp.rs b/src/cpp.rs index d03c0be..6213b8a 100644 --- a/src/cpp.rs +++ b/src/cpp.rs @@ -1,3 +1,5 @@ +//! `cpp` is the module that generates the cpp code for the bindings + use configuration::*; use configuration_private::*; use std::io::{Result, Write}; @@ -5,10 +7,9 @@ use util::{snake_case, write_if_different}; fn property_type(p: &ItemProperty) -> String { if p.optional && !p.item_property_type.is_complex() { - "QVariant".into() - } else { - p.type_name().to_string() + return "QVariant".into(); } + p.type_name().to_string() } fn upper_initial(name: &str) -> String { @@ -210,12 +211,9 @@ public: } fn is_column_write(o: &Object, col: usize) -> bool { - for ip in o.item_properties.values() { - if ip.write && (col == 0 || (ip.roles.len() > col && !ip.roles[col].is_empty())) { - return true; - } - } - false + o.item_properties + .values() + .any(|ip|ip.write && (col == 0 || (ip.roles.len() > col && !ip.roles[col].is_empty()))) } fn write_function_c_decl( diff --git a/src/rust.rs b/src/rust.rs index 3d10690..96a97c4 100644 --- a/src/rust.rs +++ b/src/rust.rs @@ -1,3 +1,5 @@ +//! `rust` is the module that generates the rust code for the binding + use configuration::*; use configuration_private::*; use std::io::{Result, Write};