Run rustfmt over everything

This commit is contained in:
Alex Crichton
2018-11-27 12:07:59 -08:00
parent 4a70198143
commit 48f4adfa8c
45 changed files with 872 additions and 800 deletions

View File

@ -1,7 +1,7 @@
use Diagnostic;
use proc_macro2::{Ident, Span};
use shared;
use syn;
use Diagnostic;
/// An abstract syntax tree representing a rust program. Contains
/// extra information for joining up this rust code with javascript.

View File

@ -1,6 +1,6 @@
use std::collections::HashSet;
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use std::sync::Mutex;
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::ToTokens;
@ -89,9 +89,11 @@ impl TryToTokens for ast::Program {
// See comments in `crates/cli-support/src/lib.rs` about what this
// `schema_version` is.
let prefix_json = format!(r#"{{"schema_version":"{}","version":"{}"}}"#,
shared::SCHEMA_VERSION,
shared::version());
let prefix_json = format!(
r#"{{"schema_version":"{}","version":"{}"}}"#,
shared::SCHEMA_VERSION,
shared::version()
);
let mut bytes = Vec::new();
bytes.push((prefix_json.len() >> 0) as u8);
bytes.push((prefix_json.len() >> 8) as u8);
@ -110,7 +112,8 @@ impl TryToTokens for ast::Program {
#[doc(hidden)]
pub static #generated_static_name: [u8; #generated_static_length] =
*#generated_static_value;
}).to_tokens(tokens);
})
.to_tokens(tokens);
Ok(())
}
@ -237,7 +240,8 @@ impl ToTokens for ast::Struct {
(*js).borrow_mut()
}
}
}).to_tokens(tokens);
})
.to_tokens(tokens);
for field in self.fields.iter() {
field.to_tokens(tokens);
@ -273,14 +277,16 @@ impl ToTokens for ast::StructField {
&mut GlobalStack::new(),
)
}
}).to_tokens(tokens);
})
.to_tokens(tokens);
Descriptor(
&getter,
quote! {
<#ty as WasmDescribe>::describe();
},
).to_tokens(tokens);
)
.to_tokens(tokens);
if self.readonly {
return;
@ -305,7 +311,8 @@ impl ToTokens for ast::StructField {
);
(*js).borrow_mut().#name = val;
}
}).to_tokens(tokens);
})
.to_tokens(tokens);
}
}
@ -461,7 +468,8 @@ impl TryToTokens for ast::Export {
};
#convert_ret
}
}).to_tokens(into);
})
.to_tokens(into);
// In addition to generating the shim function above which is what
// our generated JS will invoke, we *also* generate a "descriptor"
@ -488,7 +496,8 @@ impl TryToTokens for ast::Export {
#(<#argtys as WasmDescribe>::describe();)*
#describe_ret
},
).to_tokens(into);
)
.to_tokens(into);
Ok(())
}
@ -670,7 +679,8 @@ impl ToTokens for ast::ImportType {
self.as_ref()
}
}
}).to_tokens(tokens);
})
.to_tokens(tokens);
for superclass in self.extends.iter() {
(quote! {
impl From<#rust_name> for #superclass {
@ -688,7 +698,8 @@ impl ToTokens for ast::ImportType {
#superclass::unchecked_from_js_ref(self.as_ref())
}
}
}).to_tokens(tokens);
})
.to_tokens(tokens);
}
}
}
@ -709,7 +720,8 @@ impl ToTokens for ast::ImportEnum {
let this_index = current_idx;
current_idx += 1;
Literal::usize_unsuffixed(this_index)
}).collect();
})
.collect();
// Borrow variant_indexes because we need to use it multiple times inside the quote! macro
let variant_indexes_ref = &variant_indexes;
@ -946,7 +958,8 @@ impl TryToTokens for ast::ImportFunction {
impl #class {
#invocation
}
}).to_tokens(tokens);
})
.to_tokens(tokens);
} else {
invocation.to_tokens(tokens);
}
@ -981,7 +994,8 @@ impl<'a> ToTokens for DescribeImport<'a> {
#(<#argtys as WasmDescribe>::describe();)*
#inform_ret
},
).to_tokens(tokens);
)
.to_tokens(tokens);
}
}
@ -1025,7 +1039,8 @@ impl ToTokens for ast::Enum {
inform(ENUM);
}
}
}).to_tokens(into);
})
.to_tokens(into);
}
}
@ -1061,7 +1076,8 @@ impl ToTokens for ast::ImportStatic {
__inner: &_VAL,
}
};
}).to_tokens(into);
})
.to_tokens(into);
}
}
@ -1106,7 +1122,8 @@ impl ToTokens for ast::Const {
impl #class {
#declaration
}
}).to_tokens(tokens);
})
.to_tokens(tokens);
} else {
declaration.to_tokens(tokens);
}
@ -1247,7 +1264,8 @@ impl ToTokens for ast::Dictionary {
}
}
};
}).to_tokens(tokens);
})
.to_tokens(tokens);
}
}
@ -1309,6 +1327,7 @@ impl<'a, T: ToTokens> ToTokens for Descriptor<'a, T> {
::wasm_bindgen::__rt::link_mem_intrinsics();
#inner
}
}).to_tokens(tokens);
})
.to_tokens(tokens);
}
}

View File

@ -3,8 +3,8 @@ use std::collections::HashMap;
use proc_macro2::{Ident, Span};
use Diagnostic;
use ast;
use Diagnostic;
pub fn encode(program: &ast::Program) -> Result<Vec<u8>, Diagnostic> {
let mut e = Encoder::new();
@ -19,13 +19,15 @@ struct Interner {
impl Interner {
fn new() -> Interner {
Interner { map: RefCell::new(HashMap::new()) }
Interner {
map: RefCell::new(HashMap::new()),
}
}
fn intern(&self, s: &Ident) -> &str {
let mut map = self.map.borrow_mut();
if let Some(s) = map.get(s) {
return unsafe { &*(&**s as *const str) }
return unsafe { &*(&**s as *const str) };
}
map.insert(s.clone(), s.to_string());
unsafe { &*(&*map[s] as *const str) }
@ -36,17 +38,32 @@ impl Interner {
}
}
fn shared_program<'a>(prog: &'a ast::Program, intern: &'a Interner)
-> Result<Program<'a>, Diagnostic>
{
fn shared_program<'a>(
prog: &'a ast::Program,
intern: &'a Interner,
) -> Result<Program<'a>, Diagnostic> {
Ok(Program {
exports: prog.exports.iter().map(|a| shared_export(a, intern)).collect(),
structs: prog.structs.iter().map(|a| shared_struct(a, intern)).collect(),
exports: prog
.exports
.iter()
.map(|a| shared_export(a, intern))
.collect(),
structs: prog
.structs
.iter()
.map(|a| shared_struct(a, intern))
.collect(),
enums: prog.enums.iter().map(|a| shared_enum(a, intern)).collect(),
imports: prog.imports.iter()
imports: prog
.imports
.iter()
.map(|a| shared_import(a, intern))
.collect::<Result<Vec<_>, _>>()?,
typescript_custom_sections: prog.typescript_custom_sections.iter().map(|x| -> &'a str { &x }).collect(),
typescript_custom_sections: prog
.typescript_custom_sections
.iter()
.map(|x| -> &'a str { &x })
.collect(),
// version: shared::version(),
// schema_version: shared::SCHEMA_VERSION.to_string(),
})
@ -69,15 +86,17 @@ fn shared_export<'a>(export: &'a ast::Export, intern: &'a Interner) -> Export<'a
}
fn shared_function<'a>(func: &'a ast::Function, _intern: &'a Interner) -> Function<'a> {
Function {
name: &func.name,
}
Function { name: &func.name }
}
fn shared_enum<'a>(e: &'a ast::Enum, intern: &'a Interner) -> Enum<'a> {
Enum {
name: intern.intern(&e.name),
variants: e.variants.iter().map(|v| shared_variant(v, intern)).collect(),
variants: e
.variants
.iter()
.map(|v| shared_variant(v, intern))
.collect(),
comments: e.comments.iter().map(|s| &**s).collect(),
}
}
@ -89,9 +108,7 @@ fn shared_variant<'a>(v: &'a ast::Variant, intern: &'a Interner) -> EnumVariant<
}
}
fn shared_import<'a>(i: &'a ast::Import, intern: &'a Interner)
-> Result<Import<'a>, Diagnostic>
{
fn shared_import<'a>(i: &'a ast::Import, intern: &'a Interner) -> Result<Import<'a>, Diagnostic> {
Ok(Import {
module: i.module.as_ref().map(|s| &**s),
js_namespace: i.js_namespace.as_ref().map(|s| intern.intern(s)),
@ -99,9 +116,10 @@ fn shared_import<'a>(i: &'a ast::Import, intern: &'a Interner)
})
}
fn shared_import_kind<'a>(i: &'a ast::ImportKind, intern: &'a Interner)
-> Result<ImportKind<'a>, Diagnostic>
{
fn shared_import_kind<'a>(
i: &'a ast::ImportKind,
intern: &'a Interner,
) -> Result<ImportKind<'a>, Diagnostic> {
Ok(match i {
ast::ImportKind::Function(f) => ImportKind::Function(shared_import_function(f, intern)?),
ast::ImportKind::Static(f) => ImportKind::Static(shared_import_static(f, intern)),
@ -110,11 +128,12 @@ fn shared_import_kind<'a>(i: &'a ast::ImportKind, intern: &'a Interner)
})
}
fn shared_import_function<'a>(i: &'a ast::ImportFunction, intern: &'a Interner)
-> Result<ImportFunction<'a>, Diagnostic>
{
fn shared_import_function<'a>(
i: &'a ast::ImportFunction,
intern: &'a Interner,
) -> Result<ImportFunction<'a>, Diagnostic> {
let method = match &i.kind {
ast::ImportFunctionKind::Method { class, kind, .. } => {
ast::ImportFunctionKind::Method { class, kind, .. } => {
let kind = match kind {
ast::MethodKind::Constructor => MethodKind::Constructor,
ast::MethodKind::Operation(ast::Operation { is_static, kind }) => {
@ -123,9 +142,7 @@ fn shared_import_function<'a>(i: &'a ast::ImportFunction, intern: &'a Interner)
ast::OperationKind::Regular => OperationKind::Regular,
ast::OperationKind::Getter(g) => {
let g = g.as_ref().map(|g| intern.intern(g));
OperationKind::Getter(
g.unwrap_or_else(|| i.infer_getter_property()),
)
OperationKind::Getter(g.unwrap_or_else(|| i.infer_getter_property()))
}
ast::OperationKind::Setter(s) => {
let s = s.as_ref().map(|s| intern.intern(s));
@ -141,10 +158,7 @@ fn shared_import_function<'a>(i: &'a ast::ImportFunction, intern: &'a Interner)
MethodKind::Operation(Operation { is_static, kind })
}
};
Some(MethodData {
class,
kind,
})
Some(MethodData { class, kind })
}
ast::ImportFunctionKind::Normal => None,
};
@ -159,44 +173,38 @@ fn shared_import_function<'a>(i: &'a ast::ImportFunction, intern: &'a Interner)
})
}
fn shared_import_static<'a>(i: &'a ast::ImportStatic, intern: &'a Interner)
-> ImportStatic<'a>
{
fn shared_import_static<'a>(i: &'a ast::ImportStatic, intern: &'a Interner) -> ImportStatic<'a> {
ImportStatic {
name: &i.js_name,
shim: intern.intern(&i.shim),
}
}
fn shared_import_type<'a>(i: &'a ast::ImportType, intern: &'a Interner)
-> ImportType<'a>
{
fn shared_import_type<'a>(i: &'a ast::ImportType, intern: &'a Interner) -> ImportType<'a> {
ImportType {
name: &i.js_name,
instanceof_shim: &i.instanceof_shim,
vendor_prefixes: i.vendor_prefixes.iter()
.map(|x| intern.intern(x))
.collect(),
vendor_prefixes: i.vendor_prefixes.iter().map(|x| intern.intern(x)).collect(),
}
}
fn shared_import_enum<'a>(_i: &'a ast::ImportEnum, _intern: &'a Interner)
-> ImportEnum
{
fn shared_import_enum<'a>(_i: &'a ast::ImportEnum, _intern: &'a Interner) -> ImportEnum {
ImportEnum {}
}
fn shared_struct<'a>(s: &'a ast::Struct, intern: &'a Interner) -> Struct<'a> {
Struct {
name: &s.js_name,
fields: s.fields.iter().map(|s| shared_struct_field(s, intern)).collect(),
fields: s
.fields
.iter()
.map(|s| shared_struct_field(s, intern))
.collect(),
comments: s.comments.iter().map(|s| &**s).collect(),
}
}
fn shared_struct_field<'a>(s: &'a ast::StructField, intern: &'a Interner)
-> StructField<'a>
{
fn shared_struct_field<'a>(s: &'a ast::StructField, intern: &'a Interner) -> StructField<'a> {
StructField {
name: intern.intern(&s.name),
readonly: s.readonly,

View File

@ -1,8 +1,5 @@
#![recursion_limit = "256"]
#![cfg_attr(
feature = "extra-traits",
deny(missing_debug_implementations)
)]
#![cfg_attr(feature = "extra-traits", deny(missing_debug_implementations))]
#![doc(html_root_url = "https://docs.rs/wasm-bindgen-backend/0.2")]
#[macro_use]
@ -25,6 +22,6 @@ mod error;
pub mod ast;
mod codegen;
mod encode;
pub mod defined;
mod encode;
pub mod util;

View File

@ -71,7 +71,8 @@ where
.map(|i| syn::PathSegment {
ident: i,
arguments: syn::PathArguments::None,
}).collect();
})
.collect();
syn::TypePath {
qself: None,
@ -83,7 +84,8 @@ where
},
segments: syn::punctuated::Punctuated::from_iter(segments),
},
}.into()
}
.into()
}
pub fn ident_ty(ident: Ident) -> syn::Type {