From c837ee102fb8b27426f90b7b1908f8e9a86d02b5 Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Tue, 1 Sep 2020 13:19:44 +0200 Subject: [PATCH 1/3] migrate to rust 2018 edition --- CHANGELOG.md | 5 ++ Cargo.toml | 1 + src/associated_type.rs | 7 +-- src/block.rs | 8 ++-- src/body.rs | 14 +++--- src/bound.rs | 3 +- src/docs.rs | 6 +-- src/enum.rs | 12 ++--- src/field.rs | 7 ++- src/fields.rs | 16 +++---- src/formatter.rs | 14 ++---- src/function.rs | 20 ++++---- src/impl.rs | 14 +++--- src/import.rs | 1 - src/item.rs | 13 +++--- src/lib.rs | 1 + src/module.rs | 20 ++++---- src/scope.rs | 31 ++++++------- src/struct.rs | 17 +++---- src/trait.rs | 16 +++---- src/type.rs | 24 +++++----- src/type_def.rs | 25 +++++----- src/variant.rs | 10 ++-- tests/codegen.rs | 102 ++++++++++++++++++++++------------------- 24 files changed, 183 insertions(+), 204 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85cf789..5dea49f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# + +### Changed +- updated the crate to rust 2018 edition + # 0.1.3 (May 9, 2020) ### Added diff --git a/Cargo.toml b/Cargo.toml index c06bef1..dde02aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ documentation = "https://docs.rs/codegen/0.1.3/codegen" homepage = "https://github.com/carllerche/codegen" repository = "https://github.com/carllerche/codegen" readme = "README.md" +edition = "2018" [dependencies] indexmap = "1.0.2" diff --git a/src/associated_type.rs b/src/associated_type.rs index a9d91f5..c4d26dc 100644 --- a/src/associated_type.rs +++ b/src/associated_type.rs @@ -1,13 +1,10 @@ -use bound::Bound; - -use r#type::Type; - +use crate::bound::Bound; +use crate::r#type::Type; /// Defines an associated type. #[derive(Debug, Clone)] pub struct AssociatedType(pub Bound); - impl AssociatedType { /// Add a bound to the associated type. pub fn bound(&mut self, ty: T) -> &mut Self diff --git a/src/block.rs b/src/block.rs index 57d720e..6bb321e 100644 --- a/src/block.rs +++ b/src/block.rs @@ -1,8 +1,7 @@ use std::fmt::{self, Write}; -use body::Body; -use formatter::Formatter; - +use crate::body::Body; +use crate::formatter::Formatter; /// Defines a code block. This is used to define a function body. #[derive(Debug, Clone)] @@ -12,7 +11,6 @@ pub struct Block { body: Vec, } - impl Block { /// Returns an empty code block. pub fn new(before: &str) -> Self { @@ -45,7 +43,7 @@ impl Block { } /// Formats the block using the given formatter. - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { if let Some(ref before) = self.before { write!(fmt, "{}", before)?; } diff --git a/src/body.rs b/src/body.rs index b8d27a8..4c6efbe 100644 --- a/src/body.rs +++ b/src/body.rs @@ -1,8 +1,7 @@ use std::fmt::{self, Write}; -use block::Block; -use formatter::Formatter; - +use crate::block::Block; +use crate::formatter::Formatter; #[derive(Debug, Clone)] pub enum Body { @@ -10,12 +9,11 @@ pub enum Body { Block(Block), } - impl Body { - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { - match *self { - Body::String(ref s) => write!(fmt, "{}\n", s), - Body::Block(ref b) => b.fmt(fmt), + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { + match &self { + Body::String(s) => write!(fmt, "{}\n", s), + Body::Block(b) => b.fmt(fmt), } } } diff --git a/src/bound.rs b/src/bound.rs index 3904638..94da046 100644 --- a/src/bound.rs +++ b/src/bound.rs @@ -1,5 +1,4 @@ -use r#type::Type; - +use crate::r#type::Type; #[derive(Debug, Clone)] pub struct Bound { diff --git a/src/docs.rs b/src/docs.rs index 0fcbe21..d03dc3a 100644 --- a/src/docs.rs +++ b/src/docs.rs @@ -1,14 +1,12 @@ use std::fmt::{self, Write}; -use formatter::Formatter; - +use crate::formatter::Formatter; #[derive(Debug, Clone)] pub struct Docs { docs: String, } - impl Docs { pub fn new(docs: &str) -> Self { Docs { @@ -16,7 +14,7 @@ impl Docs { } } - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { for line in self.docs.lines() { write!(fmt, "/// {}\n", line)?; } diff --git a/src/enum.rs b/src/enum.rs index 6e08906..2636f62 100644 --- a/src/enum.rs +++ b/src/enum.rs @@ -1,11 +1,10 @@ use std::fmt; -use formatter::Formatter; -use type_def::TypeDef; -use variant::Variant; - -use r#type::Type; +use crate::formatter::Formatter; +use crate::type_def::TypeDef; +use crate::variant::Variant; +use crate::r#type::Type; /// Defines an enumeration. #[derive(Debug, Clone)] @@ -14,7 +13,6 @@ pub struct Enum { variants: Vec, } - impl Enum { /// Return a enum definition with the provided name. pub fn new(name: &str) -> Self { @@ -87,7 +85,7 @@ impl Enum { } /// Formats the enum using the given formatter. - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { self.type_def.fmt_head("enum", &[], fmt)?; fmt.block(|fmt| { diff --git a/src/field.rs b/src/field.rs index 6077f12..769158f 100644 --- a/src/field.rs +++ b/src/field.rs @@ -1,5 +1,4 @@ -use r#type::Type; - +use crate::r#type::Type; /// Defines a struct field. #[derive(Debug, Clone)] @@ -17,11 +16,11 @@ pub struct Field { pub annotation: Vec, } - impl Field { /// Return a field definition with the provided name and type pub fn new(name: &str, ty: T) -> Self - where T: Into, + where + T: Into, { Field { name: name.into(), diff --git a/src/fields.rs b/src/fields.rs index f3d6465..6878818 100644 --- a/src/fields.rs +++ b/src/fields.rs @@ -1,10 +1,9 @@ use std::fmt::{self, Write}; -use field::Field; -use formatter::Formatter; - -use r#type::Type; +use crate::field::Field; +use crate::formatter::Formatter; +use crate::r#type::Type; /// Defines a set of fields. #[derive(Debug, Clone)] @@ -14,10 +13,8 @@ pub enum Fields { Named(Vec), } - impl Fields { - pub fn push_named(&mut self, field: Field) -> &mut Self - { + pub fn push_named(&mut self, field: Field) -> &mut Self { match *self { Fields::Empty => { *self = Fields::Named(vec![field]); @@ -32,7 +29,8 @@ impl Fields { } pub fn named(&mut self, name: &str, ty: T) -> &mut Self - where T: Into, + where + T: Into, { self.push_named(Field { name: name.to_string(), @@ -59,7 +57,7 @@ impl Fields { self } - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { match *self { Fields::Named(ref fields) => { assert!(!fields.is_empty()); diff --git a/src/formatter.rs b/src/formatter.rs index aa06354..447a6f6 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -1,13 +1,11 @@ use std::fmt::{self, Write}; -use bound::Bound; - -use r#type::Type; +use crate::bound::Bound; +use crate::r#type::Type; const DEFAULT_INDENT: usize = 4; - /// Configures how a scope is formatted. #[derive(Debug)] pub struct Formatter<'a> { @@ -21,7 +19,6 @@ pub struct Formatter<'a> { indent: usize, } - impl<'a> Formatter<'a> { /// Return a new formatter that writes to the given string. pub fn new(dst: &'a mut String) -> Self { @@ -102,9 +99,8 @@ impl<'a> fmt::Write for Formatter<'a> { } } - /// Format generics. -pub fn fmt_generics(generics: &[String], fmt: &mut Formatter) -> fmt::Result { +pub fn fmt_generics(generics: &[String], fmt: &mut Formatter<'_>) -> fmt::Result { if !generics.is_empty() { write!(fmt, "<")?; @@ -122,7 +118,7 @@ pub fn fmt_generics(generics: &[String], fmt: &mut Formatter) -> fmt::Result { } /// Format generic bounds. -pub fn fmt_bounds(bounds: &[Bound], fmt: &mut Formatter) -> fmt::Result { +pub fn fmt_bounds(bounds: &[Bound], fmt: &mut Formatter<'_>) -> fmt::Result { if !bounds.is_empty() { write!(fmt, "\n")?; @@ -142,7 +138,7 @@ pub fn fmt_bounds(bounds: &[Bound], fmt: &mut Formatter) -> fmt::Result { } /// Format multiple generic bounds. -pub fn fmt_bound_rhs(tys: &[Type], fmt: &mut Formatter) -> fmt::Result { +pub fn fmt_bound_rhs(tys: &[Type], fmt: &mut Formatter<'_>) -> fmt::Result { for (i, ty) in tys.iter().enumerate() { if i != 0 { write!(fmt, " + ")? diff --git a/src/function.rs b/src/function.rs index e89fc66..827ec6c 100644 --- a/src/function.rs +++ b/src/function.rs @@ -1,15 +1,14 @@ use std::fmt::{self, Write}; -use block::Block; -use body::Body; -use bound::Bound; -use docs::Docs; -use field::Field; -use formatter::{fmt_bounds, fmt_generics}; -use formatter::Formatter; - -use r#type::Type; +use crate::block::Block; +use crate::body::Body; +use crate::bound::Bound; +use crate::docs::Docs; +use crate::field::Field; +use crate::formatter::Formatter; +use crate::formatter::{fmt_bounds, fmt_generics}; +use crate::r#type::Type; /// Defines a function. #[derive(Debug, Clone)] @@ -54,7 +53,6 @@ pub struct Function { r#async: bool, } - impl Function { /// Return a new function definition. pub fn new(name: &str) -> Self { @@ -211,7 +209,7 @@ impl Function { } /// Formats the function using the given formatter. - pub fn fmt(&self, is_trait: bool, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, is_trait: bool, fmt: &mut Formatter<'_>) -> fmt::Result { if let Some(ref docs) = self.docs { docs.fmt(fmt)?; } diff --git a/src/impl.rs b/src/impl.rs index 1aef190..6e411b5 100644 --- a/src/impl.rs +++ b/src/impl.rs @@ -1,12 +1,11 @@ use std::fmt::{self, Write}; -use bound::Bound; -use field::Field; -use formatter::{Formatter, fmt_bounds, fmt_generics}; -use function::Function; - -use r#type::Type; +use crate::bound::Bound; +use crate::field::Field; +use crate::formatter::{fmt_bounds, fmt_generics, Formatter}; +use crate::function::Function; +use crate::r#type::Type; /// Defines an impl block. #[derive(Debug, Clone)] @@ -31,7 +30,6 @@ pub struct Impl { macros: Vec, } - impl Impl { /// Return a new impl definition pub fn new(target: T) -> Self @@ -121,7 +119,7 @@ impl Impl { } /// Formats the impl block using the given formatter. - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { for m in self.macros.iter() { write!(fmt, "{}\n", m)?; } diff --git a/src/import.rs b/src/import.rs index 0cc1209..6d44bd0 100644 --- a/src/import.rs +++ b/src/import.rs @@ -7,7 +7,6 @@ pub struct Import { pub vis: Option, } - impl Import { /// Return a new import. pub fn new(path: &str, ty: &str) -> Self { diff --git a/src/item.rs b/src/item.rs index 56d3c96..5f6fb62 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,11 +1,10 @@ -use function::Function; -use module::Module; - -use r#enum::Enum; -use r#impl::Impl; -use r#struct::Struct; -use r#trait::Trait; +use crate::function::Function; +use crate::module::Module; +use crate::r#enum::Enum; +use crate::r#impl::Impl; +use crate::r#struct::Struct; +use crate::r#trait::Trait; #[derive(Debug, Clone)] pub enum Item { diff --git a/src/lib.rs b/src/lib.rs index b722f4b..af7cdb6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![deny(warnings, missing_debug_implementations, missing_docs)] #![doc(html_root_url = "https://docs.rs/codegen/0.1.1")] +#![warn(rust_2018_idioms)] //! Provides a builder API for generating Rust code. //! diff --git a/src/module.rs b/src/module.rs index da42968..4846a61 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,15 +1,14 @@ use std::fmt::{self, Write}; -use docs::Docs; -use formatter::Formatter; -use function::Function; -use scope::Scope; - -use r#enum::Enum; -use r#impl::Impl; -use r#struct::Struct; -use r#trait::Trait; +use crate::docs::Docs; +use crate::formatter::Formatter; +use crate::function::Function; +use crate::scope::Scope; +use crate::r#enum::Enum; +use crate::r#impl::Impl; +use crate::r#struct::Struct; +use crate::r#trait::Trait; /// Defines a module. #[derive(Debug, Clone)] @@ -27,7 +26,6 @@ pub struct Module { scope: Scope, } - impl Module { /// Return a new, blank module pub fn new(name: &str) -> Self { @@ -165,7 +163,7 @@ impl Module { } /// Formats the module using the given formatter. - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { if let Some(ref vis) = self.vis { write!(fmt, "{} ", vis)?; } diff --git a/src/scope.rs b/src/scope.rs index aa7bd25..1312139 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -1,22 +1,18 @@ -extern crate indexmap; - - use std::fmt::{self, Write}; -use self::indexmap::IndexMap; +use indexmap::IndexMap; -use docs::Docs; -use formatter::Formatter; -use function::Function; -use import::Import; -use item::Item; -use module::Module; - -use r#enum::Enum; -use r#impl::Impl; -use r#struct::Struct; -use r#trait::Trait; +use crate::docs::Docs; +use crate::formatter::Formatter; +use crate::function::Function; +use crate::import::Import; +use crate::item::Item; +use crate::module::Module; +use crate::r#enum::Enum; +use crate::r#impl::Impl; +use crate::r#struct::Struct; +use crate::r#trait::Trait; /// Defines a scope. /// @@ -33,7 +29,6 @@ pub struct Scope { items: Vec, } - impl Scope { /// Returns a new scope pub fn new() -> Self { @@ -239,7 +234,7 @@ impl Scope { } /// Formats the scope using the given formatter. - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { self.fmt_imports(fmt)?; if !self.imports.is_empty() { @@ -267,7 +262,7 @@ impl Scope { Ok(()) } - fn fmt_imports(&self, fmt: &mut Formatter) -> fmt::Result { + fn fmt_imports(&self, fmt: &mut Formatter<'_>) -> fmt::Result { // First, collect all visibilities let mut visibilities = vec![]; diff --git a/src/struct.rs b/src/struct.rs index ea50916..8feae8a 100644 --- a/src/struct.rs +++ b/src/struct.rs @@ -1,12 +1,11 @@ use std::fmt::{self, Write}; -use field::Field; -use fields::Fields; -use formatter::Formatter; -use type_def::TypeDef; - -use r#type::Type; +use crate::field::Field; +use crate::fields::Fields; +use crate::formatter::Formatter; +use crate::type_def::TypeDef; +use crate::r#type::Type; /// Defines a struct. #[derive(Debug, Clone)] @@ -17,7 +16,6 @@ pub struct Struct { fields: Fields, } - impl Struct { /// Return a structure definition with the provided name pub fn new(name: &str) -> Self { @@ -81,8 +79,7 @@ impl Struct { /// /// A struct can either set named fields with this function or tuple fields /// with `push_tuple_field`, but not both. - pub fn push_field(&mut self, field: Field) -> &mut Self - { + pub fn push_field(&mut self, field: Field) -> &mut Self { self.fields.push_named(field); self } @@ -112,7 +109,7 @@ impl Struct { } /// Formats the struct using the given formatter. - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { self.type_def.fmt_head("struct", &[], fmt)?; self.fields.fmt(fmt)?; diff --git a/src/trait.rs b/src/trait.rs index 1a100b1..7a8c069 100644 --- a/src/trait.rs +++ b/src/trait.rs @@ -1,13 +1,12 @@ use std::fmt::{self, Write}; -use associated_type::AssociatedType; -use bound::Bound; -use formatter::{Formatter, fmt_bound_rhs}; -use function::Function; -use type_def::TypeDef; - -use r#type::Type; +use crate::associated_type::AssociatedType; +use crate::bound::Bound; +use crate::formatter::{fmt_bound_rhs, Formatter}; +use crate::function::Function; +use crate::type_def::TypeDef; +use crate::r#type::Type; /// Define a trait. #[derive(Debug, Clone)] @@ -19,7 +18,6 @@ pub struct Trait { macros: Vec, } - impl Trait { /// Return a trait definition with the provided name pub fn new(name: &str) -> Self { @@ -106,7 +104,7 @@ impl Trait { } /// Formats the scope using the given formatter. - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { self.type_def.fmt_head("trait", &self.parents, fmt)?; fmt.block(|fmt| { diff --git a/src/type.rs b/src/type.rs index 61c4637..4a46eca 100644 --- a/src/type.rs +++ b/src/type.rs @@ -1,7 +1,6 @@ use std::fmt::{self, Write}; -use formatter::Formatter; - +use crate::formatter::Formatter; /// Defines a type. #[derive(Debug, Clone)] @@ -10,7 +9,6 @@ pub struct Type { generics: Vec, } - impl Type { /// Return a new type with the given name. pub fn new(name: &str) -> Self { @@ -53,12 +51,12 @@ impl Type { } /// Formats the struct using the given formatter. - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { write!(fmt, "{}", self.name)?; Type::fmt_slice(&self.generics, fmt) } - fn fmt_slice(generics: &[Type], fmt: &mut Formatter) -> fmt::Result { + fn fmt_slice(generics: &[Type], fmt: &mut Formatter<'_>) -> fmt::Result { if !generics.is_empty() { write!(fmt, "<")?; @@ -74,30 +72,30 @@ impl Type { Ok(()) } - } +} - impl<'a> From<&'a str> for Type { +impl<'a> From<&'a str> for Type { fn from(src: &'a str) -> Self { Type::new(src) } - } +} - impl From for Type { +impl From for Type { fn from(src: String) -> Self { Type { name: src, generics: vec![], } } - } +} - impl<'a> From<&'a String> for Type { +impl<'a> From<&'a String> for Type { fn from(src: &'a String) -> Self { Type::new(src) } - } +} - impl<'a> From<&'a Type> for Type { +impl<'a> From<&'a Type> for Type { fn from(src: &'a Type) -> Self { src.clone() } diff --git a/src/type_def.rs b/src/type_def.rs index c196cd1..a5fad01 100644 --- a/src/type_def.rs +++ b/src/type_def.rs @@ -1,11 +1,10 @@ use std::fmt::{self, Write}; -use bound::Bound; -use docs::Docs; -use formatter::{Formatter, fmt_bounds}; - -use r#type::Type; +use crate::bound::Bound; +use crate::docs::Docs; +use crate::formatter::{fmt_bounds, Formatter}; +use crate::r#type::Type; /// Defines a type definition. #[derive(Debug, Clone)] @@ -20,7 +19,6 @@ pub struct TypeDef { macros: Vec, } - impl TypeDef { /// Return a structure definition with the provided name pub fn new(name: &str) -> Self { @@ -70,7 +68,12 @@ impl TypeDef { self.repr = Some(repr.to_string()); } - pub fn fmt_head(&self, keyword: &str, parents: &[Type], fmt: &mut Formatter) -> fmt::Result { + pub fn fmt_head( + &self, + keyword: &str, + parents: &[Type], + fmt: &mut Formatter<'_>, + ) -> fmt::Result { if let Some(ref docs) = self.docs { docs.fmt(fmt)?; } @@ -104,7 +107,7 @@ impl TypeDef { Ok(()) } - fn fmt_allow(&self, fmt: &mut Formatter) -> fmt::Result { + fn fmt_allow(&self, fmt: &mut Formatter<'_>) -> fmt::Result { if let Some(ref allow) = self.allow { write!(fmt, "#[allow({})]\n", allow)?; } @@ -112,7 +115,7 @@ impl TypeDef { Ok(()) } - fn fmt_repr(&self, fmt: &mut Formatter) -> fmt::Result { + fn fmt_repr(&self, fmt: &mut Formatter<'_>) -> fmt::Result { if let Some(ref repr) = self.repr { write!(fmt, "#[repr({})]\n", repr)?; } @@ -120,7 +123,7 @@ impl TypeDef { Ok(()) } - fn fmt_derive(&self, fmt: &mut Formatter) -> fmt::Result { + fn fmt_derive(&self, fmt: &mut Formatter<'_>) -> fmt::Result { if !self.derive.is_empty() { write!(fmt, "#[derive(")?; @@ -137,7 +140,7 @@ impl TypeDef { Ok(()) } - fn fmt_macros(&self, fmt: &mut Formatter) -> fmt::Result { + fn fmt_macros(&self, fmt: &mut Formatter<'_>) -> fmt::Result { for m in self.macros.iter() { write!(fmt, "{}\n", m)?; } diff --git a/src/variant.rs b/src/variant.rs index b522a55..164f2e4 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -1,10 +1,9 @@ use std::fmt::{self, Write}; -use fields::Fields; -use formatter::Formatter; - -use r#type::Type; +use crate::fields::Fields; +use crate::formatter::Formatter; +use crate::r#type::Type; /// Defines an enum variant. #[derive(Debug, Clone)] @@ -13,7 +12,6 @@ pub struct Variant { fields: Fields, } - impl Variant { /// Return a new enum variant with the given name. pub fn new(name: &str) -> Self { @@ -39,7 +37,7 @@ impl Variant { } /// Formats the variant using the given formatter. - pub fn fmt(&self, fmt: &mut Formatter) -> fmt::Result { + pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { write!(fmt, "{}", self.name)?; self.fields.fmt(fmt)?; write!(fmt, ",\n")?; diff --git a/tests/codegen.rs b/tests/codegen.rs index c8c0306..b48bd1c 100644 --- a/tests/codegen.rs +++ b/tests/codegen.rs @@ -1,6 +1,5 @@ -extern crate codegen; - use codegen::*; + #[test] fn empty_scope() { let scope = Scope::new(); @@ -12,7 +11,8 @@ fn empty_scope() { fn single_struct() { let mut scope = Scope::new(); - scope.new_struct("Foo") + scope + .new_struct("Foo") .field("one", "usize") .field("two", "String"); @@ -86,7 +86,8 @@ struct Foo { #[test] fn single_fn() { let mut scope = Scope::new(); - scope.new_fn("my_fn") + scope + .new_fn("my_fn") .vis("pub") .arg("foo", Type::new("uint")) .ret(Type::new("uint")) @@ -118,12 +119,12 @@ struct Foo;"#; fn two_structs() { let mut scope = Scope::new(); - scope.new_struct("Foo") + scope + .new_struct("Foo") .field("one", "usize") .field("two", "String"); - scope.new_struct("Bar") - .field("hello", "World"); + scope.new_struct("Bar").field("hello", "World"); let expect = r#" struct Foo { @@ -142,8 +143,10 @@ struct Bar { fn struct_with_derive() { let mut scope = Scope::new(); - scope.new_struct("Foo") - .derive("Debug").derive("Clone") + scope + .new_struct("Foo") + .derive("Debug") + .derive("Clone") .field("one", "usize") .field("two", "String"); @@ -161,7 +164,8 @@ struct Foo { fn struct_with_repr() { let mut scope = Scope::new(); - scope.new_struct("Foo") + scope + .new_struct("Foo") .repr("C") .field("one", "u8") .field("two", "u8"); @@ -180,7 +184,8 @@ struct Foo { fn struct_with_allow() { let mut scope = Scope::new(); - scope.new_struct("Foo") + scope + .new_struct("Foo") .allow("dead_code") .field("one", "u8") .field("two", "u8"); @@ -199,7 +204,8 @@ struct Foo { fn struct_with_generics_1() { let mut scope = Scope::new(); - scope.new_struct("Foo") + scope + .new_struct("Foo") .generic("T") .generic("U") .field("one", "T") @@ -218,7 +224,8 @@ struct Foo { fn struct_with_generics_2() { let mut scope = Scope::new(); - scope.new_struct("Foo") + scope + .new_struct("Foo") .generic("T, U") .field("one", "T") .field("two", "U"); @@ -236,7 +243,8 @@ struct Foo { fn struct_with_generics_3() { let mut scope = Scope::new(); - scope.new_struct("Foo") + scope + .new_struct("Foo") .generic("T: Win, U") .field("one", "T") .field("two", "U"); @@ -254,7 +262,8 @@ struct Foo { fn struct_where_clause_1() { let mut scope = Scope::new(); - scope.new_struct("Foo") + scope + .new_struct("Foo") .generic("T") .bound("T", "Foo") .field("one", "T"); @@ -273,7 +282,8 @@ where T: Foo, fn struct_where_clause_2() { let mut scope = Scope::new(); - scope.new_struct("Foo") + scope + .new_struct("Foo") .generic("T, U") .bound("T", "Foo") .bound("U", "Baz") @@ -296,9 +306,12 @@ where T: Foo, fn struct_doc() { let mut scope = Scope::new(); - scope.new_struct("Foo") - .doc("Hello, this is a doc string\n\ - that continues on another line.") + scope + .new_struct("Foo") + .doc( + "Hello, this is a doc string\n\ + that continues on another line.", + ) .field("one", "T"); let expect = r#" @@ -317,15 +330,15 @@ fn struct_in_mod() { { let module = scope.new_module("foo"); - module.new_struct("Foo") + module + .new_struct("Foo") .doc("Hello some docs") .derive("Debug") .generic("T, U") .bound("T", "SomeBound") .bound("U", "SomeOtherBound") .field("one", "T") - .field("two", "U") - ; + .field("two", "U"); } let expect = r#" @@ -347,11 +360,11 @@ mod foo { #[test] fn struct_mod_import() { let mut scope = Scope::new(); - scope.new_module("foo") + scope + .new_module("foo") .import("bar", "Bar") .new_struct("Foo") - .field("bar", "Bar") - ; + .field("bar", "Bar"); let expect = r#" mod foo { @@ -369,11 +382,11 @@ mod foo { fn enum_with_repr() { let mut scope = Scope::new(); - scope.new_enum("IpAddrKind") + scope + .new_enum("IpAddrKind") .repr("u8") .push_variant(Variant::new("V4")) - .push_variant(Variant::new("V6")) - ; + .push_variant(Variant::new("V6")); let expect = r#" #[repr(u8)] @@ -389,11 +402,11 @@ enum IpAddrKind { fn enum_with_allow() { let mut scope = Scope::new(); - scope.new_enum("IpAddrKind") + scope + .new_enum("IpAddrKind") .allow("dead_code") .push_variant(Variant::new("V4")) - .push_variant(Variant::new("V6")) - ; + .push_variant(Variant::new("V6")); let expect = r#" #[allow(dead_code)] @@ -408,15 +421,15 @@ enum IpAddrKind { #[test] fn scoped_imports() { let mut scope = Scope::new(); - scope.new_module("foo") + scope + .new_module("foo") .import("bar", "Bar") .import("bar", "baz::Baz") .import("bar::quux", "quuux::Quuuux") .new_struct("Foo") .field("bar", "Bar") .field("baz", "baz::Baz") - .field("quuuux", "quuux::Quuuux") - ; + .field("quuuux", "quuux::Quuuux"); let expect = r#" mod foo { @@ -436,14 +449,13 @@ mod foo { #[test] fn module_mut() { let mut scope = Scope::new(); - scope.new_module("foo") - .import("bar", "Bar") - ; + scope.new_module("foo").import("bar", "Bar"); - scope.get_module_mut("foo").expect("module_mut") + scope + .get_module_mut("foo") + .expect("module_mut") .new_struct("Foo") - .field("bar", "Bar") - ; + .field("bar", "Bar"); let expect = r#" mod foo { @@ -462,14 +474,12 @@ fn get_or_new_module() { let mut scope = Scope::new(); assert!(scope.get_module("foo").is_none()); - scope.get_or_new_module("foo") - .import("bar", "Bar") - ; + scope.get_or_new_module("foo").import("bar", "Bar"); - scope.get_or_new_module("foo") + scope + .get_or_new_module("foo") .new_struct("Foo") - .field("bar", "Bar") - ; + .field("bar", "Bar"); let expect = r#" mod foo { From 6275642359938f6fa3dcebaaaad9edbafc5cb00f Mon Sep 17 00:00:00 2001 From: Luro02 <24826124+Luro02@users.noreply.github.com> Date: Tue, 1 Sep 2020 13:22:20 +0200 Subject: [PATCH 2/3] remove deny(warnings) (anti-pattern) see https://www.reddit.com/r/rust/comments/f5xpib/psa_denywarnings_is_actively_harmful/ --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index af7cdb6..70b8675 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -#![deny(warnings, missing_debug_implementations, missing_docs)] +#![deny(missing_debug_implementations, missing_docs)] #![doc(html_root_url = "https://docs.rs/codegen/0.1.1")] #![warn(rust_2018_idioms)] From 6a743c6e1df9c076881b13e659307b16e2a0abf0 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 13 Sep 2020 11:35:45 +0200 Subject: [PATCH 3/3] fixed many clippy lints --- src/block.rs | 8 ++++---- src/body.rs | 4 ++-- src/docs.rs | 4 ++-- src/enum.rs | 4 ++-- src/field.rs | 2 +- src/fields.rs | 30 +++++++++++++++--------------- src/formatter.rs | 14 +++++++------- src/function.rs | 24 ++++++++++++------------ src/impl.rs | 8 ++++---- src/import.rs | 2 +- src/lib.rs | 3 +-- src/module.rs | 12 ++++++------ src/scope.rs | 42 +++++++++++++++++++++--------------------- src/struct.rs | 8 ++++---- src/trait.rs | 8 ++++---- src/type.rs | 32 ++++++++++++++++---------------- src/type_def.rs | 10 +++++----- src/variant.rs | 4 ++-- 18 files changed, 109 insertions(+), 110 deletions(-) diff --git a/src/block.rs b/src/block.rs index 6bb321e..25d5bbe 100644 --- a/src/block.rs +++ b/src/block.rs @@ -14,7 +14,7 @@ pub struct Block { impl Block { /// Returns an empty code block. pub fn new(before: &str) -> Self { - Block { + Self { before: Some(before.to_string()), after: None, body: vec![], @@ -31,7 +31,7 @@ impl Block { } /// Push a nested block to this block. - pub fn push_block(&mut self, block: Block) -> &mut Self { + pub fn push_block(&mut self, block: Self) -> &mut Self { self.body.push(Body::Block(block)); self } @@ -54,7 +54,7 @@ impl Block { write!(fmt, " ")?; } - write!(fmt, "{{\n")?; + writeln!(fmt, "{{")?; fmt.indent(|fmt| { for b in &self.body { @@ -70,7 +70,7 @@ impl Block { write!(fmt, "{}", after)?; } - write!(fmt, "\n")?; + writeln!(fmt)?; Ok(()) } } diff --git a/src/body.rs b/src/body.rs index 4c6efbe..bfad635 100644 --- a/src/body.rs +++ b/src/body.rs @@ -12,8 +12,8 @@ pub enum Body { impl Body { pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { match &self { - Body::String(s) => write!(fmt, "{}\n", s), - Body::Block(b) => b.fmt(fmt), + Self::String(s) => writeln!(fmt, "{}", s), + Self::Block(b) => b.fmt(fmt), } } } diff --git a/src/docs.rs b/src/docs.rs index d03dc3a..815f244 100644 --- a/src/docs.rs +++ b/src/docs.rs @@ -9,14 +9,14 @@ pub struct Docs { impl Docs { pub fn new(docs: &str) -> Self { - Docs { + Self { docs: docs.to_string(), } } pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { for line in self.docs.lines() { - write!(fmt, "/// {}\n", line)?; + writeln!(fmt, "/// {}", line)?; } Ok(()) diff --git a/src/enum.rs b/src/enum.rs index 2636f62..01da7bb 100644 --- a/src/enum.rs +++ b/src/enum.rs @@ -16,14 +16,14 @@ pub struct Enum { impl Enum { /// Return a enum definition with the provided name. pub fn new(name: &str) -> Self { - Enum { + Self { type_def: TypeDef::new(name), variants: vec![], } } /// Returns a reference to the type. - pub fn ty(&self) -> &Type { + pub const fn ty(&self) -> &Type { &self.type_def.ty } diff --git a/src/field.rs b/src/field.rs index 769158f..8f61a89 100644 --- a/src/field.rs +++ b/src/field.rs @@ -22,7 +22,7 @@ impl Field { where T: Into, { - Field { + Self { name: name.into(), ty: ty.into(), documentation: Vec::new(), diff --git a/src/fields.rs b/src/fields.rs index 6878818..ef7d8fe 100644 --- a/src/fields.rs +++ b/src/fields.rs @@ -15,11 +15,11 @@ pub enum Fields { impl Fields { pub fn push_named(&mut self, field: Field) -> &mut Self { - match *self { - Fields::Empty => { - *self = Fields::Named(vec![field]); + match self { + Self::Empty => { + *self = Self::Named(vec![field]); } - Fields::Named(ref mut fields) => { + Self::Named(fields) => { fields.push(field); } _ => panic!("field list is named"), @@ -44,11 +44,11 @@ impl Fields { where T: Into, { - match *self { - Fields::Empty => { - *self = Fields::Tuple(vec![ty.into()]); + match self { + Self::Empty => { + *self = Self::Tuple(vec![ty.into()]); } - Fields::Tuple(ref mut fields) => { + Self::Tuple(fields) => { fields.push(ty.into()); } _ => panic!("field list is tuple"), @@ -58,31 +58,31 @@ impl Fields { } pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { - match *self { - Fields::Named(ref fields) => { + match &self { + Self::Named(fields) => { assert!(!fields.is_empty()); fmt.block(|fmt| { for f in fields { if !f.documentation.is_empty() { for doc in &f.documentation { - write!(fmt, "/// {}\n", doc)?; + writeln!(fmt, "/// {}", doc)?; } } if !f.annotation.is_empty() { for ann in &f.annotation { - write!(fmt, "{}\n", ann)?; + writeln!(fmt, "{}", ann)?; } } write!(fmt, "{}: ", f.name)?; f.ty.fmt(fmt)?; - write!(fmt, ",\n")?; + writeln!(fmt, ",")?; } Ok(()) })?; } - Fields::Tuple(ref tys) => { + Self::Tuple(tys) => { assert!(!tys.is_empty()); write!(fmt, "(")?; @@ -96,7 +96,7 @@ impl Fields { write!(fmt, ")")?; } - Fields::Empty => {} + Self::Empty => {} } Ok(()) diff --git a/src/formatter.rs b/src/formatter.rs index 447a6f6..a73bbba 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -22,7 +22,7 @@ pub struct Formatter<'a> { impl<'a> Formatter<'a> { /// Return a new formatter that writes to the given string. pub fn new(dst: &'a mut String) -> Self { - Formatter { + Self { dst, spaces: 0, indent: DEFAULT_INDENT, @@ -38,9 +38,9 @@ impl<'a> Formatter<'a> { write!(self, " ")?; } - write!(self, "{{\n")?; + writeln!(self, "{{")?; self.indent(f)?; - write!(self, "}}\n")?; + writeln!(self, "}}")?; Ok(()) } @@ -57,7 +57,7 @@ impl<'a> Formatter<'a> { /// Check if current destination is the start of a new line. pub fn is_start_of_line(&self) -> bool { - self.dst.is_empty() || self.dst.as_bytes().last() == Some(&b'\n') + self.dst.is_empty() || self.dst.ends_with('\n') } fn push_spaces(&mut self) { @@ -120,17 +120,17 @@ pub fn fmt_generics(generics: &[String], fmt: &mut Formatter<'_>) -> fmt::Result /// Format generic bounds. pub fn fmt_bounds(bounds: &[Bound], fmt: &mut Formatter<'_>) -> fmt::Result { if !bounds.is_empty() { - write!(fmt, "\n")?; + writeln!(fmt)?; // Write first bound write!(fmt, "where {}: ", bounds[0].name)?; fmt_bound_rhs(&bounds[0].bound, fmt)?; - write!(fmt, ",\n")?; + writeln!(fmt, ",")?; for bound in &bounds[1..] { write!(fmt, " {}: ", bound.name)?; fmt_bound_rhs(&bound.bound, fmt)?; - write!(fmt, ",\n")?; + writeln!(fmt, ",")?; } } diff --git a/src/function.rs b/src/function.rs index 827ec6c..a59db41 100644 --- a/src/function.rs +++ b/src/function.rs @@ -56,7 +56,7 @@ pub struct Function { impl Function { /// Return a new function definition. pub fn new(name: &str) -> Self { - Function { + Self { name: name.to_string(), docs: None, allow: None, @@ -210,16 +210,16 @@ impl Function { /// Formats the function using the given formatter. pub fn fmt(&self, is_trait: bool, fmt: &mut Formatter<'_>) -> fmt::Result { - if let Some(ref docs) = self.docs { + if let Some(docs) = &self.docs { docs.fmt(fmt)?; } - if let Some(ref allow) = self.allow { - write!(fmt, "#[allow({})]\n", allow)?; + if let Some(allow) = &self.allow { + writeln!(fmt, "#[allow({})]", allow)?; } for attr in self.attributes.iter() { - write!(fmt, "#[{}]\n", attr)?; + writeln!(fmt, "#[{}]", attr)?; } if is_trait { @@ -229,11 +229,11 @@ impl Function { ); } - if let Some(ref vis) = self.vis { + if let Some(vis) = &self.vis { write!(fmt, "{} ", vis)?; } - if let Some(ref extern_abi) = self.extern_abi { + if let Some(extern_abi) = &self.extern_abi { write!(fmt, "extern \"{extern_abi}\" ", extern_abi = extern_abi)?; } @@ -246,7 +246,7 @@ impl Function { write!(fmt, "(")?; - if let Some(ref s) = self.arg_self { + if let Some(s) = &self.arg_self { write!(fmt, "{}", s)?; } @@ -261,15 +261,15 @@ impl Function { write!(fmt, ")")?; - if let Some(ref ret) = self.ret { + if let Some(ret) = &self.ret { write!(fmt, " -> ")?; ret.fmt(fmt)?; } fmt_bounds(&self.bounds, fmt)?; - match self.body { - Some(ref body) => fmt.block(|fmt| { + match &self.body { + Some(body) => fmt.block(|fmt| { for b in body { b.fmt(fmt)?; } @@ -281,7 +281,7 @@ impl Function { panic!("impl blocks must define fn bodies"); } - write!(fmt, ";\n") + writeln!(fmt, ";") } } } diff --git a/src/impl.rs b/src/impl.rs index 6e411b5..4157ff9 100644 --- a/src/impl.rs +++ b/src/impl.rs @@ -36,7 +36,7 @@ impl Impl { where T: Into, { - Impl { + Self { target: target.into(), generics: vec![], impl_trait: None, @@ -121,7 +121,7 @@ impl Impl { /// Formats the impl block using the given formatter. pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { for m in self.macros.iter() { - write!(fmt, "{}\n", m)?; + writeln!(fmt, "{}", m)?; } write!(fmt, "impl")?; fmt_generics(&self.generics[..], fmt)?; @@ -143,13 +143,13 @@ impl Impl { for ty in &self.assoc_tys { write!(fmt, "type {} = ", ty.name)?; ty.ty.fmt(fmt)?; - write!(fmt, ";\n")?; + writeln!(fmt, ";")?; } } for (i, func) in self.fns.iter().enumerate() { if i != 0 || !self.assoc_tys.is_empty() { - write!(fmt, "\n")?; + writeln!(fmt)?; } func.fmt(false, fmt)?; diff --git a/src/import.rs b/src/import.rs index 6d44bd0..1d943b3 100644 --- a/src/import.rs +++ b/src/import.rs @@ -10,7 +10,7 @@ pub struct Import { impl Import { /// Return a new import. pub fn new(path: &str, ty: &str) -> Self { - Import { + Self { line: format!("{}::{}", path, ty), vis: None, } diff --git a/src/lib.rs b/src/lib.rs index 70b8675..7be6aa2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ #![deny(missing_debug_implementations, missing_docs)] #![doc(html_root_url = "https://docs.rs/codegen/0.1.1")] -#![warn(rust_2018_idioms)] +#![warn(rust_2018_idioms, clippy::nursery)] //! Provides a builder API for generating Rust code. //! @@ -47,7 +47,6 @@ mod r#struct; mod r#trait; mod r#type; - pub use associated_type::*; pub use block::*; pub use field::*; diff --git a/src/module.rs b/src/module.rs index 4846a61..5c96388 100644 --- a/src/module.rs +++ b/src/module.rs @@ -29,7 +29,7 @@ pub struct Module { impl Module { /// Return a new, blank module pub fn new(name: &str) -> Self { - Module { + Self { name: name.to_string(), vis: None, docs: None, @@ -69,12 +69,12 @@ impl Module { /// will return the existing definition instead. /// /// [`get_or_new_module`]: #method.get_or_new_module - pub fn new_module(&mut self, name: &str) -> &mut Module { + pub fn new_module(&mut self, name: &str) -> &mut Self { self.scope.new_module(name) } /// Returns a reference to a module if it is exists in this scope. - pub fn get_module(&self, name: &Q) -> Option<&Module> + pub fn get_module(&self, name: &Q) -> Option<&Self> where String: PartialEq, { @@ -82,7 +82,7 @@ impl Module { } /// Returns a mutable reference to a module if it is exists in this scope. - pub fn get_module_mut(&mut self, name: &Q) -> Option<&mut Module> + pub fn get_module_mut(&mut self, name: &Q) -> Option<&mut Self> where String: PartialEq, { @@ -91,7 +91,7 @@ impl Module { /// Returns a mutable reference to a module, creating it if it does /// not exist. - pub fn get_or_new_module(&mut self, name: &str) -> &mut Module { + pub fn get_or_new_module(&mut self, name: &str) -> &mut Self { self.scope.get_or_new_module(name) } @@ -107,7 +107,7 @@ impl Module { /// return the existing definition instead. /// /// [`get_or_new_module`]: #method.get_or_new_module - pub fn push_module(&mut self, item: Module) -> &mut Self { + pub fn push_module(&mut self, item: Self) -> &mut Self { self.scope.push_module(item); self } diff --git a/src/scope.rs b/src/scope.rs index 1312139..c5a36ea 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -32,7 +32,7 @@ pub struct Scope { impl Scope { /// Returns a new scope pub fn new() -> Self { - Scope { + Self { docs: None, imports: IndexMap::new(), items: vec![], @@ -69,8 +69,8 @@ impl Scope { pub fn new_module(&mut self, name: &str) -> &mut Module { self.push_module(Module::new(name)); - match *self.items.last_mut().unwrap() { - Item::Module(ref mut v) => v, + match self.items.last_mut().unwrap() { + Item::Module(v) => v, _ => unreachable!(), } } @@ -83,7 +83,7 @@ impl Scope { self.items .iter_mut() .filter_map(|item| match item { - &mut Item::Module(ref mut module) if module.name == *name => Some(module), + Item::Module(ref mut module) if module.name == *name => Some(module), _ => None, }) .next() @@ -97,7 +97,7 @@ impl Scope { self.items .iter() .filter_map(|item| match item { - &Item::Module(ref module) if module.name == *name => Some(module), + Item::Module(module) if module.name == *name => Some(module), _ => None, }) .next() @@ -135,8 +135,8 @@ impl Scope { pub fn new_struct(&mut self, name: &str) -> &mut Struct { self.push_struct(Struct::new(name)); - match *self.items.last_mut().unwrap() { - Item::Struct(ref mut v) => v, + match self.items.last_mut().unwrap() { + Item::Struct(v) => v, _ => unreachable!(), } } @@ -238,23 +238,23 @@ impl Scope { self.fmt_imports(fmt)?; if !self.imports.is_empty() { - write!(fmt, "\n")?; + writeln!(fmt)?; } for (i, item) in self.items.iter().enumerate() { if i != 0 { - write!(fmt, "\n")?; + writeln!(fmt)?; } - match *item { - Item::Module(ref v) => v.fmt(fmt)?, - Item::Struct(ref v) => v.fmt(fmt)?, - Item::Function(ref v) => v.fmt(false, fmt)?, - Item::Trait(ref v) => v.fmt(fmt)?, - Item::Enum(ref v) => v.fmt(fmt)?, - Item::Impl(ref v) => v.fmt(fmt)?, - Item::Raw(ref v) => { - write!(fmt, "{}\n", v)?; + match &item { + Item::Module(v) => v.fmt(fmt)?, + Item::Struct(v) => v.fmt(fmt)?, + Item::Function(v) => v.fmt(false, fmt)?, + Item::Trait(v) => v.fmt(fmt)?, + Item::Enum(v) => v.fmt(fmt)?, + Item::Impl(v) => v.fmt(fmt)?, + Item::Raw(v) => { + writeln!(fmt, "{}", v)?; } } } @@ -288,7 +288,7 @@ impl Scope { } if !tys.is_empty() { - if let Some(ref vis) = *vis { + if let Some(vis) = &vis { write!(fmt, "{} ", vis)?; } @@ -304,9 +304,9 @@ impl Scope { write!(fmt, "{}", ty)?; } - write!(fmt, "}};\n")?; + writeln!(fmt, "}};")?; } else if tys.len() == 1 { - write!(fmt, "{};\n", tys[0])?; + writeln!(fmt, "{};", tys[0])?; } } } diff --git a/src/struct.rs b/src/struct.rs index 8feae8a..9d1ca1e 100644 --- a/src/struct.rs +++ b/src/struct.rs @@ -19,14 +19,14 @@ pub struct Struct { impl Struct { /// Return a structure definition with the provided name pub fn new(name: &str) -> Self { - Struct { + Self { type_def: TypeDef::new(name), fields: Fields::Empty, } } /// Returns a reference to the type - pub fn ty(&self) -> &Type { + pub const fn ty(&self) -> &Type { &self.type_def.ty } @@ -115,10 +115,10 @@ impl Struct { match self.fields { Fields::Empty => { - write!(fmt, ";\n")?; + writeln!(fmt, ";")?; } Fields::Tuple(..) => { - write!(fmt, ";\n")?; + writeln!(fmt, ";")?; } _ => {} } diff --git a/src/trait.rs b/src/trait.rs index 7a8c069..58b3363 100644 --- a/src/trait.rs +++ b/src/trait.rs @@ -21,7 +21,7 @@ pub struct Trait { impl Trait { /// Return a trait definition with the provided name pub fn new(name: &str) -> Self { - Trait { + Self { type_def: TypeDef::new(name), parents: vec![], associated_tys: vec![], @@ -31,7 +31,7 @@ impl Trait { } /// Returns a reference to the type - pub fn ty(&self) -> &Type { + pub const fn ty(&self) -> &Type { &self.type_def.ty } @@ -122,13 +122,13 @@ impl Trait { fmt_bound_rhs(&ty.bound, fmt)?; } - write!(fmt, ";\n")?; + writeln!(fmt, ";")?; } } for (i, func) in self.fns.iter().enumerate() { if i != 0 || !assoc.is_empty() { - write!(fmt, "\n")?; + writeln!(fmt)?; } func.fmt(true, fmt)?; diff --git a/src/type.rs b/src/type.rs index 4a46eca..d0a15a2 100644 --- a/src/type.rs +++ b/src/type.rs @@ -12,7 +12,7 @@ pub struct Type { impl Type { /// Return a new type with the given name. pub fn new(name: &str) -> Self { - Type { + Self { name: name.to_string(), generics: vec![], } @@ -21,11 +21,11 @@ impl Type { /// Add a generic to the type. pub fn generic(&mut self, ty: T) -> &mut Self where - T: Into, + T: Into, { // Make sure that the name doesn't already include generics assert!( - !self.name.contains("<"), + !self.name.contains('<'), "type name already includes generics" ); @@ -36,7 +36,7 @@ impl Type { /// Rewrite the `Type` with the provided path /// /// TODO: Is this needed? - pub fn path(&self, path: &str) -> Type { + pub fn path(&self, path: &str) -> Self { // TODO: This isn't really correct assert!(!self.name.contains("::")); @@ -44,7 +44,7 @@ impl Type { name.push_str("::"); name.push_str(&self.name); - Type { + Self { name, generics: self.generics.clone(), } @@ -53,10 +53,10 @@ impl Type { /// Formats the struct using the given formatter. pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { write!(fmt, "{}", self.name)?; - Type::fmt_slice(&self.generics, fmt) + Self::fmt_slice(&self.generics, fmt) } - fn fmt_slice(generics: &[Type], fmt: &mut Formatter<'_>) -> fmt::Result { + fn fmt_slice(generics: &[Self], fmt: &mut Formatter<'_>) -> fmt::Result { if !generics.is_empty() { write!(fmt, "<")?; @@ -74,29 +74,29 @@ impl Type { } } -impl<'a> From<&'a str> for Type { - fn from(src: &'a str) -> Self { - Type::new(src) +impl From<&str> for Type { + fn from(src: &str) -> Self { + Self::new(src) } } impl From for Type { fn from(src: String) -> Self { - Type { + Self { name: src, generics: vec![], } } } -impl<'a> From<&'a String> for Type { - fn from(src: &'a String) -> Self { - Type::new(src) +impl From<&String> for Type { + fn from(src: &String) -> Self { + Self::new(src) } } -impl<'a> From<&'a Type> for Type { - fn from(src: &'a Type) -> Self { +impl From<&Type> for Type { + fn from(src: &Self) -> Self { src.clone() } } diff --git a/src/type_def.rs b/src/type_def.rs index a5fad01..0f84bcc 100644 --- a/src/type_def.rs +++ b/src/type_def.rs @@ -22,7 +22,7 @@ pub struct TypeDef { impl TypeDef { /// Return a structure definition with the provided name pub fn new(name: &str) -> Self { - TypeDef { + Self { ty: Type::new(name), vis: None, docs: None, @@ -109,7 +109,7 @@ impl TypeDef { fn fmt_allow(&self, fmt: &mut Formatter<'_>) -> fmt::Result { if let Some(ref allow) = self.allow { - write!(fmt, "#[allow({})]\n", allow)?; + writeln!(fmt, "#[allow({})]", allow)?; } Ok(()) @@ -117,7 +117,7 @@ impl TypeDef { fn fmt_repr(&self, fmt: &mut Formatter<'_>) -> fmt::Result { if let Some(ref repr) = self.repr { - write!(fmt, "#[repr({})]\n", repr)?; + writeln!(fmt, "#[repr({})]", repr)?; } Ok(()) @@ -134,7 +134,7 @@ impl TypeDef { write!(fmt, "{}", name)?; } - write!(fmt, ")]\n")?; + writeln!(fmt, ")]")?; } Ok(()) @@ -142,7 +142,7 @@ impl TypeDef { fn fmt_macros(&self, fmt: &mut Formatter<'_>) -> fmt::Result { for m in self.macros.iter() { - write!(fmt, "{}\n", m)?; + writeln!(fmt, "{}", m)?; } Ok(()) } diff --git a/src/variant.rs b/src/variant.rs index 164f2e4..3edfea8 100644 --- a/src/variant.rs +++ b/src/variant.rs @@ -15,7 +15,7 @@ pub struct Variant { impl Variant { /// Return a new enum variant with the given name. pub fn new(name: &str) -> Self { - Variant { + Self { name: name.to_string(), fields: Fields::Empty, } @@ -40,7 +40,7 @@ impl Variant { pub fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { write!(fmt, "{}", self.name)?; self.fields.fmt(fmt)?; - write!(fmt, ",\n")?; + writeln!(fmt, ",")?; Ok(()) }