Skip to content

Commit

Permalink
Stop dereferencing followed by reborrowing in match and if let
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed May 14, 2021
1 parent c148542 commit 1be73d0
Show file tree
Hide file tree
Showing 21 changed files with 214 additions and 275 deletions.
6 changes: 3 additions & 3 deletions src/analysis/conversion_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ impl ConversionType {
UIntPtr => ConversionType::Direct,
Unsupported => ConversionType::Unknown,
},
Alias(ref alias) if alias.c_identifier == "GQuark" => ConversionType::Scalar,
Alias(ref alias) => ConversionType::of(env, alias.typ),
Alias(alias) if alias.c_identifier == "GQuark" => ConversionType::Scalar,
Alias(alias) => ConversionType::of(env, alias.typ),
Bitfield(_) => ConversionType::Scalar,
Record(_) => ConversionType::Pointer,
Union(_) => ConversionType::Pointer,
Expand All @@ -92,7 +92,7 @@ impl ConversionType {
List(_) => ConversionType::Pointer,
SList(_) => ConversionType::Pointer,
PtrArray(_) => ConversionType::Pointer,
Function(super::library::Function { ref name, .. }) if name == "AsyncReadyCallback" => {
Function(super::library::Function { name, .. }) if name == "AsyncReadyCallback" => {
ConversionType::Direct
}
Function(_) => ConversionType::Direct,
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/ffi_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn ffi_type(env: &Env, tid: TypeId, c_type: &str) -> Result {

fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result {
let typ = env.library.type_(tid);
match *typ {
match typ {
Type::Fundamental(fund) => {
use crate::library::Fundamental::*;
let inner = match fund {
Expand Down Expand Up @@ -127,8 +127,8 @@ fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result {
}
fix_name(env, tid, inner)
}
Type::CArray(inner_tid) => ffi_inner(env, inner_tid, inner),
Type::FixedArray(inner_tid, size, _) => ffi_inner(env, inner_tid, inner)
&Type::CArray(inner_tid) => ffi_inner(env, inner_tid, inner),
&Type::FixedArray(inner_tid, size, _) => ffi_inner(env, inner_tid, inner)
.map_any(|rust_type| rust_type.alter_type(|typ_| format!("[{}; {}]", typ_, size))),
Type::Array(..)
| Type::PtrArray(..)
Expand Down
10 changes: 3 additions & 7 deletions src/analysis/function_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ impl TransformationType {
}

pub fn set_to_glib_extra(&mut self, to_glib_extra_: &str) {
if let TransformationType::ToGlibPointer {
ref mut to_glib_extra,
..
} = *self
{
if let TransformationType::ToGlibPointer { to_glib_extra, .. } = self {
*to_glib_extra = to_glib_extra_.to_owned();
}
}
Expand Down Expand Up @@ -458,7 +454,7 @@ fn is_length(par: &library::Parameter) -> bool {
fn has_length(env: &Env, typ: TypeId) -> bool {
use crate::library::Type;
let typ = env.library.type_(typ);
match *typ {
match typ {
Type::Fundamental(fund) => {
use crate::library::Fundamental::*;
matches!(fund, Utf8 | Filename | OsString)
Expand All @@ -470,7 +466,7 @@ fn has_length(env: &Env, typ: TypeId) -> bool {
| Type::List(..)
| Type::SList(..)
| Type::HashTable(..) => true,
Type::Alias(ref alias) => has_length(env, alias.typ),
Type::Alias(alias) => has_length(env, alias.typ),
_ => false,
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/analysis/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub fn analyze<F: Borrow<library::Function>>(
}
}
}
if let Some(ref mut signatures) = signatures {
if let Some(signatures) = signatures.as_mut() {
signatures.insert(name.clone(), signature_params);
}

Expand Down Expand Up @@ -998,7 +998,7 @@ fn analyze_callback(
) -> Option<(Trampoline, Option<usize>)> {
let mut imports_to_add = Vec::new();

if let Type::Function(ref func) = rust_type {
if let Type::Function(func) = rust_type {
if par.c_type != "GDestroyNotify" {
if let Some(user_data) = par.user_data_index {
if user_data >= c_parameters.len() {
Expand Down Expand Up @@ -1186,11 +1186,11 @@ pub fn find_function<'a>(env: &'a Env, c_identifier: &str) -> Option<&'a Functio
return Some(f);
}
for typ in &namespace.types {
if let Some(Type::Class(ref class)) = *typ {
if let Some(Type::Class(class)) = typ {
if let Some(f) = find(&class.functions) {
return Some(f);
}
} else if let Some(Type::Interface(ref interface)) = *typ {
} else if let Some(Type::Interface(interface)) = typ {
if let Some(f) = find(&interface.functions) {
return Some(f);
}
Expand Down
14 changes: 5 additions & 9 deletions src/analysis/out_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,12 @@ pub fn analyze_imports<'a>(
}

fn analyze_type_imports(env: &Env, typ: TypeId, caller_allocates: bool, imports: &mut Imports) {
match *env.library.type_(typ) {
Type::Alias(ref alias) => analyze_type_imports(env, alias.typ, caller_allocates, imports),
match env.library.type_(typ) {
Type::Alias(alias) => analyze_type_imports(env, alias.typ, caller_allocates, imports),
Type::Bitfield(..) | Type::Enumeration(..) => imports.add("std::mem"),
Type::Fundamental(fund)
if fund != Fundamental::Utf8
&& fund != Fundamental::OsString
&& fund != Fundamental::Filename =>
{
imports.add("std::mem")
}
Type::Fundamental(Fundamental::Utf8)
| Type::Fundamental(Fundamental::OsString)
| Type::Fundamental(Fundamental::Filename) => imports.add("std::mem"),
_ if !caller_allocates => match ConversionType::of(env, typ) {
ConversionType::Direct
| ConversionType::Scalar
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/ref_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl RefMode {
}

use crate::library::Type::*;
match *library.type_(tid) {
match library.type_(tid) {
Fundamental(library::Fundamental::Utf8)
| Fundamental(library::Fundamental::Filename)
| Fundamental(library::Fundamental::OsString)
Expand All @@ -49,7 +49,7 @@ impl RefMode {
RefMode::None
}
}
Record(ref record) => {
Record(record) => {
if direction == library::ParameterDirection::In {
if let RecordType::Refcounted = RecordType::of(record) {
RefMode::ByRef
Expand All @@ -67,7 +67,7 @@ impl RefMode {
RefMode::None
}
}
Alias(ref alias) => RefMode::of(env, alias.typ, direction),
Alias(alias) => RefMode::of(env, alias.typ, direction),
_ => RefMode::None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/return_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ pub fn analyze(

fn can_be_nullable_return(env: &Env, type_id: library::TypeId) -> bool {
use crate::library::{Fundamental::*, Type::*};
match *env.library.type_(type_id) {
match env.library.type_(type_id) {
Fundamental(fund) => matches!(fund, Pointer | Utf8 | Filename | OsString),
Alias(ref alias) => can_be_nullable_return(env, alias.typ),
Alias(alias) => can_be_nullable_return(env, alias.typ),
Enumeration(_) => false,
Bitfield(_) => false,
Record(_) => true,
Expand Down
40 changes: 20 additions & 20 deletions src/analysis/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl<'env> RustTypeBuilder<'env> {
let err = |s: &str| Err(TypeError::Unimplemented(s.into()));
let mut skip_option = false;
let type_ = self.env.library.type_(self.type_id);
let mut rust_type = match *type_ {
let mut rust_type = match type_ {
Fundamental(fund) => {
match fund {
None => err("()"),
Expand Down Expand Up @@ -299,7 +299,7 @@ impl<'env> RustTypeBuilder<'env> {
_ => err(&format!("Fundamental: {:?}", fund)),
}
}
Alias(ref alias) => {
Alias(alias) => {
RustType::try_new_and_use(self.env, self.type_id).and_then(|alias_rust_type| {
RustType::builder(&self.env, alias.typ)
.with_direction(self.direction)
Expand All @@ -312,7 +312,7 @@ impl<'env> RustTypeBuilder<'env> {
.map_any(|_| alias_rust_type)
})
}
Record(library::Record { ref c_type, .. }) if c_type == "GVariantType" => {
Record(library::Record { c_type, .. }) if c_type == "GVariantType" => {
let type_name = if self.ref_mode.is_ref() {
"VariantTy"
} else {
Expand All @@ -333,7 +333,7 @@ impl<'env> RustTypeBuilder<'env> {
}
})
}
List(inner_tid) | SList(inner_tid) | CArray(inner_tid) | PtrArray(inner_tid)
&List(inner_tid) | &SList(inner_tid) | &CArray(inner_tid) | &PtrArray(inner_tid)
if ConversionType::of(self.env, inner_tid) == ConversionType::Pointer =>
{
skip_option = true;
Expand All @@ -356,10 +356,10 @@ impl<'env> RustTypeBuilder<'env> {
})
})
}
CArray(inner_tid)
&CArray(inner_tid)
if ConversionType::of(self.env, inner_tid) == ConversionType::Direct =>
{
if let Fundamental(fund) = *self.env.library.type_(inner_tid) {
if let Fundamental(fund) = self.env.library.type_(inner_tid) {
let array_type = match fund {
Int8 => Some("i8"),
UInt8 => Some("u8"),
Expand Down Expand Up @@ -392,10 +392,10 @@ impl<'env> RustTypeBuilder<'env> {
Err(TypeError::Unimplemented(type_.get_name()))
}
}
Custom(library::Custom { ref name, .. }) => {
Custom(library::Custom { name, .. }) => {
RustType::try_new_and_use_with_name(&self.env, self.type_id, name)
}
Function(ref f) => {
Function(f) => {
let concurrency = match self.concurrency {
_ if self.scope.is_call() => "",
library::Concurrency::Send | library::Concurrency::SendUnique => " + Send",
Expand Down Expand Up @@ -601,21 +601,21 @@ impl<'env> RustTypeBuilder<'env> {
.with_scope(self.scope)
.with_try_from_glib(&self.try_from_glib)
.try_build();
match *type_ {
Fundamental(fund) => {
if (fund == library::Fundamental::Utf8
|| fund == library::Fundamental::OsString
|| fund == library::Fundamental::Filename)
&& (self.direction == ParameterDirection::InOut
|| (self.direction == ParameterDirection::Out
&& self.ref_mode == RefMode::ByRefMut))
{
return Err(TypeError::Unimplemented(into_inner(rust_type)));
}
match type_ {
Fundamental(library::Fundamental::Utf8)
| Fundamental(library::Fundamental::OsString)
| Fundamental(library::Fundamental::Filename)
if (self.direction == ParameterDirection::InOut
|| (self.direction == ParameterDirection::Out
&& self.ref_mode == RefMode::ByRefMut)) =>
{
Err(TypeError::Unimplemented(into_inner(rust_type)))
}
Fundamental(_) => {
rust_type.map_any(|rust_type| rust_type.format_parameter(self.direction))
}

Alias(ref alias) => rust_type
Alias(alias) => rust_type
.and_then(|rust_type| {
RustType::builder(&self.env, alias.typ)
.with_direction(self.direction)
Expand Down
40 changes: 19 additions & 21 deletions src/analysis/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,22 @@ pub fn run(library: &Library, namespaces: &namespaces::Info) -> Info {
id: pos as u32,
};

match *typ {
Type::Alias(Alias {
ref c_identifier, ..
}) => {
match typ {
Type::Alias(Alias { c_identifier, .. }) => {
info.insert(c_identifier, symbol, Some(tid));
}
Type::Enumeration(Enumeration {
ref name,
ref c_type,
ref members,
ref functions,
name,
c_type,
members,
functions,
..
})
| Type::Bitfield(Bitfield {
ref name,
ref c_type,
ref members,
ref functions,
name,
c_type,
members,
functions,
..
}) => {
info.insert(c_type, symbol, Some(tid));
Expand All @@ -184,21 +182,21 @@ pub fn run(library: &Library, namespaces: &namespaces::Info) -> Info {
}
}
Type::Record(Record {
ref name,
ref c_type,
ref functions,
name,
c_type,
functions,
..
})
| Type::Class(Class {
ref name,
ref c_type,
ref functions,
name,
c_type,
functions,
..
})
| Type::Interface(Interface {
ref name,
ref c_type,
ref functions,
name,
c_type,
functions,
..
}) => {
info.insert(c_type, symbol, Some(tid));
Expand Down
24 changes: 12 additions & 12 deletions src/analysis/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ impl IsIncomplete for TypeId {

impl IsIncomplete for Type {
fn is_incomplete(&self, lib: &Library) -> bool {
match *self {
Type::Fundamental(ref fundamental) => fundamental.is_incomplete(lib),
Type::Alias(ref alias) => alias.is_incomplete(lib),
match self {
Type::Fundamental(fundamental) => fundamental.is_incomplete(lib),
Type::Alias(alias) => alias.is_incomplete(lib),
Type::FixedArray(tid, ..) => tid.is_incomplete(lib),
Type::Class(ref klass) => klass.is_incomplete(lib),
Type::Record(ref record) => record.is_incomplete(lib),
Type::Union(ref union) => union.is_incomplete(lib),
Type::Function(ref function) => function.is_incomplete(lib),
Type::Class(klass) => klass.is_incomplete(lib),
Type::Record(record) => record.is_incomplete(lib),
Type::Union(union) => union.is_incomplete(lib),
Type::Function(function) => function.is_incomplete(lib),
Type::Interface(..) => true,
Type::Custom(..)
| Type::Enumeration(..)
Expand Down Expand Up @@ -206,11 +206,11 @@ impl IsExternal for Alias {

impl IsExternal for Type {
fn is_external(&self, lib: &Library) -> bool {
match *self {
Type::Alias(ref alias) => alias.is_external(lib),
Type::Class(ref klass) => klass.is_external(lib),
Type::Record(ref record) => record.is_external(lib),
Type::Union(ref union) => union.is_external(lib),
match self {
Type::Alias(alias) => alias.is_external(lib),
Type::Class(klass) => klass.is_external(lib),
Type::Record(record) => record.is_external(lib),
Type::Union(union) => union.is_external(lib),
Type::Interface(..) => true,
Type::Custom(..)
| Type::Fundamental(..)
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {

mod_rs.push("\nmod alias;".into());
for config in &configs {
if let Type::Alias(ref alias) = *env.library.type_(config.type_id.unwrap()) {
if let Type::Alias(alias) = env.library.type_(config.type_id.unwrap()) {
mod_rs.push(format!("pub use self::alias::{};", alias.name));
generate_alias(env, w, alias, config)?;
}
Expand Down
Loading

0 comments on commit 1be73d0

Please sign in to comment.