summaryrefslogtreecommitdiffstats
path: root/third_party/rust/smart-default
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--third_party/rust/smart-default/.cargo-checksum.json1
-rwxr-xr-xthird_party/rust/smart-default/CHANGELOG.md50
-rw-r--r--third_party/rust/smart-default/Cargo.lock47
-rw-r--r--third_party/rust/smart-default/Cargo.toml34
-rwxr-xr-xthird_party/rust/smart-default/LICENSE21
-rwxr-xr-xthird_party/rust/smart-default/README.md40
-rwxr-xr-xthird_party/rust/smart-default/examples/example.rs32
-rwxr-xr-xthird_party/rust/smart-default/src/body_impl.rs130
-rwxr-xr-xthird_party/rust/smart-default/src/default_attr.rs125
-rwxr-xr-xthird_party/rust/smart-default/src/lib.rs84
-rwxr-xr-xthird_party/rust/smart-default/src/util.rs29
-rwxr-xr-xthird_party/rust/smart-default/tests/tests.rs162
12 files changed, 755 insertions, 0 deletions
diff --git a/third_party/rust/smart-default/.cargo-checksum.json b/third_party/rust/smart-default/.cargo-checksum.json
new file mode 100644
index 0000000000..1fb932ce0d
--- /dev/null
+++ b/third_party/rust/smart-default/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"CHANGELOG.md":"18a0878db9cd4b571d96ec9f582a180072021d533fdae77316da1fef7649aa4f","Cargo.lock":"ec38d0341d418be606de310e72ab4018db002f8934654658c9c7c80cc39e624a","Cargo.toml":"e90e0d90e30a2b31b5b74dfdce525e2ae6c3945f132013b60a8318e873ead662","LICENSE":"f09111a2bf85257f1d098de0095ad77fd12649fbbe9a33c8a13e422848b601bb","README.md":"4acd18bb4f05c9e05fba459083e1b88901ee8f21a8cef90182c80f9224b811e8","examples/example.rs":"5bf4455ad1383bd8df85b4390bbb9fbe79aa3ed13bcefcddffccb8e91f95ba8e","src/body_impl.rs":"fa74f4354564c066a5ad9cea649e1de799019e362f95313f04f73af96fcbac16","src/default_attr.rs":"0f387d8aed64c28028d0ed798dea0671f5320ff738319cec2881bb7cb9e2536c","src/lib.rs":"4bb6e761b55b0ff50b0edc9bec561770b1ca8b487d370c420d3c0d6744590f66","src/util.rs":"ffa40f7845d63d10ac86ab495b07ac2605aa59288bf31ba37105a2bf61522984","tests/tests.rs":"1f02783a2b3e955675049b3027215a63b012aacf340dd4a5eca73d028cf3947c"},"package":"133659a15339456eeeb07572eb02a91c91e9815e9cbc89566944d2c8d3efdbf6"} \ No newline at end of file
diff --git a/third_party/rust/smart-default/CHANGELOG.md b/third_party/rust/smart-default/CHANGELOG.md
new file mode 100755
index 0000000000..6046c7791e
--- /dev/null
+++ b/third_party/rust/smart-default/CHANGELOG.md
@@ -0,0 +1,50 @@
+# Changelog
+All notable changes to this project will be documented in this file.
+
+The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
+and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
+
+## [Unreleased]
+
+## 0.6.0 - 2019-12-13
+### Changed
+- Update `syn`, `quote` and `proc-macro2` versions to `1.*.*`.
+
+## 0.5.2 - 2019-04-13
+### Fixed
+- Omit linting of generated code by adding `#[automatically_derived]` attribute.
+
+## 0.5.1 - 2019-03-01
+### Fixed
+- Don't use a multi-pattern `if let`, as this unnecessarily requires version
+ 1.33.0 of Rust.
+
+## 0.5.0 - 2019-03-01
+### Changed
+- When the default is a string literal, strap an `.into()` after it to
+ automatically convert it to `String` when needed.
+
+## 0.4.0 - 2019-02-19
+### Added
+- `#[default(_code = "...")]` syntax for defaults that cannot be parsed as
+ attributes no matter what.
+
+## 0.3.0 - 2018-11-02
+### Changed
+- Require Rust 1.30+.
+- Use direct attribute value instead of having to wrap them in strings.
+- Moved the docs from the module level to the custom derive.
+
+### Added
+- `#[default(...)]` syntax in addition to `#[default = ...]`. This is required
+ to deal with some parsing limitations.
+
+## 0.2.0 - 2017-08-21
+### Added
+- Support generic types.
+- Generate doc for the trait impl that describes the default values.
+
+## 0.1.0 - 2017-08-18
+### Added
+- Custom derive `SmartDefault` for implementing the `Default` trait.
+- `#[default = ...]` attribute on fields for smart-default.
diff --git a/third_party/rust/smart-default/Cargo.lock b/third_party/rust/smart-default/Cargo.lock
new file mode 100644
index 0000000000..d0ef699b01
--- /dev/null
+++ b/third_party/rust/smart-default/Cargo.lock
@@ -0,0 +1,47 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "proc-macro2"
+version = "1.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "smart-default"
+version = "0.6.0"
+dependencies = [
+ "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[metadata]
+"checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27"
+"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
+"checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238"
+"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
diff --git a/third_party/rust/smart-default/Cargo.toml b/third_party/rust/smart-default/Cargo.toml
new file mode 100644
index 0000000000..80913a5e6c
--- /dev/null
+++ b/third_party/rust/smart-default/Cargo.toml
@@ -0,0 +1,34 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies
+#
+# If you believe there's an error in this file please file an
+# issue against the rust-lang/cargo repository. If you're
+# editing this file be aware that the upstream Cargo.toml
+# will likely look very different (and much more reasonable)
+
+[package]
+name = "smart-default"
+version = "0.6.0"
+authors = ["IdanArye <idanarye@gmail.com>"]
+description = "Rust custom-derive macro for Default with more control on the fields"
+documentation = "https://idanarye.github.io/rust-smart-default/"
+readme = "README.md"
+keywords = ["default"]
+license = "MIT"
+repository = "https://github.com/idanarye/rust-smart-default"
+
+[lib]
+proc-macro = true
+[dependencies.proc-macro2]
+version = "1"
+
+[dependencies.quote]
+version = "1"
+
+[dependencies.syn]
+version = "1"
+features = ["full"]
diff --git a/third_party/rust/smart-default/LICENSE b/third_party/rust/smart-default/LICENSE
new file mode 100755
index 0000000000..015f0b2fcc
--- /dev/null
+++ b/third_party/rust/smart-default/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2017 Idan Arye
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/third_party/rust/smart-default/README.md b/third_party/rust/smart-default/README.md
new file mode 100755
index 0000000000..226a9cbe14
--- /dev/null
+++ b/third_party/rust/smart-default/README.md
@@ -0,0 +1,40 @@
+[![Build Status](https://api.travis-ci.org/idanarye/rust-smart-default.svg?branch=master)](https://travis-ci.org/idanarye/rust-smart-default)
+[![Latest Version](https://img.shields.io/crates/v/smart-default.svg)](https://crates.io/crates/smart-default)
+[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://idanarye.github.io/rust-smart-default/)
+
+# Rust SmartDefault
+
+Custom derive for automatically implementing the `Default` trait with customized default values:
+
+```rust
+#[macro_use]
+extern crate smart_default;
+
+#[derive(SmartDefault)]
+enum Foo {
+ Bar,
+ #[default]
+ Baz {
+ #[default = 12]
+ a: i32,
+ b: i32,
+ #[default(Some(Default::default()))]
+ c: Option<i32>,
+ #[default(_code = "vec![1, 2, 3]")]
+ d: Vec<u32>,
+ #[default = "four"]
+ e: String,
+ },
+ Qux(i32),
+}
+
+assert!(Foo::default() == Foo::Baz {
+ a: 12,
+ b: 0,
+ c: Some(0),
+ d: vec![1, 2, 3],
+ e: "four".to_owned(),
+});
+```
+
+Requires Rust 1.30+ (for non-string values in attributes)
diff --git a/third_party/rust/smart-default/examples/example.rs b/third_party/rust/smart-default/examples/example.rs
new file mode 100755
index 0000000000..aa82f999a0
--- /dev/null
+++ b/third_party/rust/smart-default/examples/example.rs
@@ -0,0 +1,32 @@
+extern crate smart_default;
+
+use smart_default::SmartDefault;
+
+#[derive(PartialEq, SmartDefault, Debug)]
+#[allow(dead_code)]
+enum Foo {
+ Bar,
+ #[default]
+ Baz {
+ #[default(12)]
+ a: i32,
+ b: i32,
+ #[default(Some(Default::default()))]
+ c: Option<i32>,
+ #[default(_code = "vec![1, 2, 3]")]
+ d: Vec<u32>,
+ #[default = "four"]
+ e: String,
+ },
+ Qux(i32),
+}
+
+fn main() {
+ assert!(Foo::default() == Foo::Baz {
+ a: 12,
+ b: 0,
+ c: Some(0),
+ d: vec![1, 2, 3],
+ e: "four".to_owned(),
+ });
+}
diff --git a/third_party/rust/smart-default/src/body_impl.rs b/third_party/rust/smart-default/src/body_impl.rs
new file mode 100755
index 0000000000..a40b11f562
--- /dev/null
+++ b/third_party/rust/smart-default/src/body_impl.rs
@@ -0,0 +1,130 @@
+use proc_macro2::TokenStream;
+
+use syn::DeriveInput;
+use syn::spanned::Spanned;
+use syn::parse::Error;
+use quote::quote;
+
+use default_attr::{DefaultAttr, ConversionStrategy};
+use util::find_only;
+
+pub fn impl_my_derive(input: &DeriveInput) -> Result<TokenStream, Error> {
+ let name = &input.ident;
+ let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
+
+ let (default_expr, doc) = match input.data {
+ syn::Data::Struct(ref body) => {
+ let (body_assignment, doc) = default_body_tt(&body.fields)?;
+ (quote! {
+ #name #body_assignment
+ }, format!("Return `{}{}`", name, doc))
+ }
+ syn::Data::Enum(ref body) => {
+ let default_variant = find_only(body.variants.iter(), |variant| {
+ if let Some(meta) = DefaultAttr::find_in_attributes(&variant.attrs)? {
+ if meta.code.is_none() {
+ Ok(true)
+ } else {
+ Err(Error::new(meta.code.span(), "Attribute #[default] on variants should have no value"))
+ }
+ } else {
+ Ok(false)
+ }
+ })?.ok_or_else(|| Error::new(input.span(), "No default variant"))?;
+ let default_variant_name = &default_variant.ident;
+ let (body_assignment, doc) = default_body_tt(&default_variant.fields)?;
+ (quote! {
+ #name :: #default_variant_name #body_assignment
+ }, format!("Return `{}::{}{}`", name, default_variant_name, doc))
+ }
+ syn::Data::Union(_) => {
+ panic!()
+ }
+ };
+ Ok(quote! {
+ #[automatically_derived]
+ impl #impl_generics Default for #name #ty_generics #where_clause {
+ #[doc = #doc]
+ fn default() -> Self {
+ #default_expr
+ }
+ }
+ })
+}
+
+/// Return a token-tree for the default "body" - the part after the name that contains the values.
+/// That is, the `{ ... }` part for structs, the `(...)` part for tuples, and nothing for units.
+fn default_body_tt(body: &syn::Fields) -> Result<(TokenStream, String), Error> {
+ let mut doc = String::new();
+ use std::fmt::Write;
+ let body_tt = match body {
+ &syn::Fields::Named(ref fields) => {
+ doc.push_str(" {");
+ let result = {
+ let field_assignments = fields.named.iter().map(|field| {
+ let field_name = field.ident.as_ref();
+ let (default_value, default_doc) = field_default_expr_and_doc(field)?;
+ write!(&mut doc, "\n {}: {},", field_name.expect("field value in struct is empty"), default_doc).unwrap();
+ // let default_value = default_value.into_token_stream();
+ Ok(quote! { #field_name : #default_value })
+ }).collect::<Result<Vec<_>, Error>>()?;
+ quote!{
+ {
+ #( #field_assignments ),*
+ }
+ }
+ };
+ if (&mut doc).ends_with(",") {
+ doc.pop();
+ doc.push('\n');
+ };
+ doc.push('}');
+ result
+ }
+ &syn::Fields::Unnamed(ref fields) => {
+ doc.push('(');
+ let result = {
+ let field_assignments = fields.unnamed.iter().map(|field| {
+ let (default_value, default_doc) = field_default_expr_and_doc(field)?;
+ write!(&mut doc, "{}, ", default_doc).unwrap();
+ Ok(default_value)
+ }).collect::<Result<Vec<TokenStream>, Error>>()?;
+ quote! {
+ (
+ #( #field_assignments ),*
+ )
+ }
+ };
+ if (&mut doc).ends_with(", ") {
+ doc.pop();
+ doc.pop();
+ };
+ doc.push(')');
+ result
+ }
+ &syn::Fields::Unit => quote!{},
+ };
+ Ok((body_tt, doc))
+}
+
+/// Return a default expression for a field based on it's `#[default = "..."]` attribute. Panic
+/// if there is more than one, of if there is a `#[default]` attribute without value.
+fn field_default_expr_and_doc(field: &syn::Field) -> Result<(TokenStream, String), Error> {
+ if let Some(default_attr) = DefaultAttr::find_in_attributes(&field.attrs)? {
+ let conversion_strategy = default_attr.conversion_strategy();
+ let field_value = default_attr.code.ok_or_else(|| {
+ Error::new(field.span(), "Expected #[default = ...] or #[default(...)]")})?;
+
+ let field_value = match conversion_strategy {
+ ConversionStrategy::NoConversion => field_value,
+ ConversionStrategy::Into => quote!((#field_value).into()),
+ };
+
+ let field_doc = format!("{}", field_value);
+ Ok((field_value, field_doc))
+ } else {
+ Ok((quote! {
+ Default::default()
+ }, "Default::default()".to_owned()))
+ }
+}
diff --git a/third_party/rust/smart-default/src/default_attr.rs b/third_party/rust/smart-default/src/default_attr.rs
new file mode 100755
index 0000000000..c429b23bb9
--- /dev/null
+++ b/third_party/rust/smart-default/src/default_attr.rs
@@ -0,0 +1,125 @@
+use proc_macro2::TokenStream;
+use syn::parse::Error;
+use syn::spanned::Spanned;
+use quote::ToTokens;
+
+use util::{find_only, single_value};
+
+#[derive(Debug, Clone, Copy)]
+pub enum ConversionStrategy {
+ NoConversion,
+ Into,
+}
+
+pub struct DefaultAttr {
+ pub code: Option<TokenStream>,
+ conversion_strategy: Option<ConversionStrategy>,
+}
+
+impl DefaultAttr {
+ pub fn find_in_attributes(attrs: &[syn::Attribute]) -> Result<Option<Self>, Error> {
+ if let Some(default_attr) = find_only(attrs.iter(), |attr| is_default_attr(attr))? {
+ match default_attr.parse_meta() {
+ Ok(syn::Meta::Path(_)) => Ok(Some(Self {
+ code: None,
+ conversion_strategy: None,
+ })),
+ Ok(syn::Meta::List(meta)) => {
+ if let Some(field_value) = parse_code_hack(&meta)? { // #[default(_code = "...")]
+ Ok(Some(Self {
+ code: Some(field_value.into_token_stream()),
+ conversion_strategy: Some(ConversionStrategy::NoConversion),
+ }))
+ } else if let Some(field_value) = single_value(meta.nested.iter()) { // #[default(...)]
+ Ok(Some(Self {
+ code: Some(field_value.into_token_stream()),
+ conversion_strategy: None,
+ }))
+ } else {
+ return Err(Error::new(
+ if meta.nested.is_empty() {
+ meta.span()
+ } else {
+ meta.nested.span()
+ },
+ "Expected signle value in #[default(...)]"));
+ }
+ }
+ Ok(syn::Meta::NameValue(meta)) => {
+ Ok(Some(Self {
+ code: Some(meta.lit.into_token_stream()),
+ conversion_strategy: None,
+ }))
+ }
+ Err(error) => {
+ if let syn::Expr::Paren(as_parens) = syn::parse(default_attr.tokens.clone().into())? {
+ Ok(Some(Self {
+ code: Some(as_parens.expr.into_token_stream()),
+ conversion_strategy: None,
+ }))
+ } else {
+ Err(error)
+ }
+ }
+ }
+ } else {
+ Ok(None)
+ }
+ }
+
+ pub fn conversion_strategy(&self) -> ConversionStrategy {
+ if let Some(conversion_strategy) = self.conversion_strategy {
+ // Conversion strategy already set
+ return conversion_strategy;
+ }
+ let code = if let Some(code) = &self.code {
+ code
+ } else {
+ // #[default] - so no conversion (`Default::default()` already has the correct type)
+ return ConversionStrategy::NoConversion;
+ };
+ match syn::parse::<syn::Lit>(code.clone().into()) {
+ Ok(syn::Lit::Str(_)) | Ok(syn::Lit::ByteStr(_))=> {
+ // A string literal - so we need a conversion in case we need to make it a `String`
+ return ConversionStrategy::Into;
+ },
+ _ => {},
+ }
+ // Not handled by one of the rules, so we don't convert it to avoid causing trouble
+ ConversionStrategy::NoConversion
+ }
+}
+
+fn is_default_attr(attr: &syn::Attribute) -> Result<bool, Error> {
+ let path = &attr.path;
+ if path.leading_colon.is_some() {
+ return Ok(false);
+ }
+ let segment = if let Some(segment) = single_value(path.segments.iter()) {
+ segment
+ } else {
+ return Ok(false);
+ };
+
+ if let syn::PathArguments::None = segment.arguments {
+ } else {
+ return Ok(false);
+ }
+
+ Ok(segment.ident.to_string() == "default")
+}
+
+fn parse_code_hack(meta: &syn::MetaList) -> Result<Option<TokenStream>, Error> {
+ for meta in meta.nested.iter() {
+ if let syn::NestedMeta::Meta(syn::Meta::NameValue(meta)) = meta {
+ if !meta.path.is_ident("_code") {
+ continue;
+ }
+ if let syn::Lit::Str(lit) = &meta.lit {
+ use std::str::FromStr;
+ return Ok(Some(TokenStream::from_str(&lit.value())?));
+ }
+ };
+ }
+ Ok(None)
+}
diff --git a/third_party/rust/smart-default/src/lib.rs b/third_party/rust/smart-default/src/lib.rs
new file mode 100755
index 0000000000..650883226c
--- /dev/null
+++ b/third_party/rust/smart-default/src/lib.rs
@@ -0,0 +1,84 @@
+extern crate proc_macro;
+extern crate proc_macro2;
+extern crate syn;
+
+extern crate quote;
+
+use syn::{parse_macro_input, DeriveInput};
+
+mod body_impl;
+mod default_attr;
+mod util;
+
+/// # Smart Default
+///
+/// This crate provides a custom derive for `SmartDefault`. `SmartDefault` is not a real type -
+/// deriving it will actually `impl Default`. The difference from regular `#[derive(Default)]` is
+/// that `#[derive(SmartDefault)]` allows you to use `#[default = "..."]` attributes to customize
+/// the `::default()` method and to support `struct`s that don't have `Default` for all their
+/// fields - and even `enum`s!
+///
+/// # Examples
+///
+/// ```
+/// #[macro_use]
+/// extern crate smart_default;
+///
+/// # fn main() {
+/// #[derive(SmartDefault)]
+/// # #[derive(PartialEq)]
+/// # #[allow(dead_code)]
+/// enum Foo {
+/// Bar,
+/// #[default]
+/// Baz {
+/// #[default = 12]
+/// a: i32,
+/// b: i32,
+/// #[default(Some(Default::default()))]
+/// c: Option<i32>,
+/// #[default(_code = "vec![1, 2, 3]")]
+/// d: Vec<u32>,
+/// #[default = "four"]
+/// e: String,
+/// },
+/// Qux(i32),
+/// }
+///
+/// assert!(Foo::default() == Foo::Baz {
+/// a: 12,
+/// b: 0,
+/// c: Some(0),
+/// d: vec![1, 2, 3],
+/// e: "four".to_owned(),
+/// });
+/// # }
+/// ```
+///
+/// * `Baz` has the `#[default]` attribute. This means that the default `Foo` is a `Foo::Baz`. Only
+/// one variant may have a `#[default]` attribute, and that attribute must have no value.
+/// * `a` has a `#[default = 12]` attribute. This means that it's default value is `12`.
+/// * `b` has no `#[default = ...]` attribute. It's default value will `i32`'s default value
+/// instead - `0`.
+/// * `c` is an `Option<i32>`, and it's default is `Some(Default::default())`. Rust cannot (currently)
+/// parse `#[default = Some(Default::default())]` and therefore we have to use a special syntax:
+/// `#[default(Some(Default::default))]`
+/// * `d` has the `!` token in it, which cannot (currently) be parsed even with `#[default(...)]`,
+/// so we have to encode it as a string and mark it as `_code = `.
+/// * `e` is a `String`, so the string literal "four" is automatically converted to it. This
+/// automatic conversion **only** happens to string (or byte string) literals - and only if
+/// `_code` is not used.
+/// * Documentation for the `impl Default` section is generated automatically, specifying the
+/// default value returned from `::default()`.
+#[proc_macro_derive(SmartDefault, attributes(default))]
+pub fn derive_smart_default(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
+ let input = parse_macro_input!(input as DeriveInput);
+ match body_impl::impl_my_derive(&input) {
+ Ok(output) => {
+ output.into()
+ },
+ Err(error) =>{
+ error.to_compile_error().into()
+ }
+ }
+}
diff --git a/third_party/rust/smart-default/src/util.rs b/third_party/rust/smart-default/src/util.rs
new file mode 100755
index 0000000000..452ac91f95
--- /dev/null
+++ b/third_party/rust/smart-default/src/util.rs
@@ -0,0 +1,29 @@
+use syn::spanned::Spanned;
+use syn::parse::Error;
+
+/// Return the value that fulfills the predicate if there is one in the slice. Panic if there is
+/// more than one.
+pub fn find_only<T, F>(iter: impl Iterator<Item = T>, pred: F) -> Result<Option<T>, Error>
+where T: Spanned,
+ F: Fn(&T) -> Result<bool, Error>,
+{
+ let mut result = None;
+ for item in iter {
+ if pred(&item)? {
+ if result.is_some() {
+ return Err(Error::new(item.span(), "Multiple defaults"));
+ }
+ result = Some(item);
+ }
+ }
+ Ok(result)
+}
+
+pub fn single_value<T>(mut it: impl Iterator<Item = T>) -> Option<T> {
+ if let Some(result) = it.next() {
+ if it.next().is_none() {
+ return Some(result)
+ }
+ }
+ None
+}
diff --git a/third_party/rust/smart-default/tests/tests.rs b/third_party/rust/smart-default/tests/tests.rs
new file mode 100755
index 0000000000..ec9f3b05d8
--- /dev/null
+++ b/third_party/rust/smart-default/tests/tests.rs
@@ -0,0 +1,162 @@
+#[macro_use]
+extern crate smart_default;
+
+#[test]
+fn test_unit() {
+ #[derive(PartialEq, SmartDefault)]
+ struct Foo;
+
+ assert!(Foo::default() == Foo);
+}
+
+#[test]
+fn test_tuple() {
+ #[derive(PartialEq, SmartDefault)]
+ struct Foo (
+ #[default = 10]
+ i32,
+ #[default = 20]
+ i32,
+ // No default
+ i32,
+ );
+
+ assert!(Foo::default() == Foo(10, 20, 0));
+}
+
+#[test]
+fn test_struct() {
+ #[derive(PartialEq, SmartDefault)]
+ struct Foo {
+ #[default = 10]
+ x: i32,
+ #[default = 20]
+ y: i32,
+ // No default
+ z: i32,
+ }
+
+ assert!(Foo::default() == Foo { x: 10, y: 20, z: 0 });
+}
+
+#[test]
+fn test_enum_of_units() {
+ #[derive(PartialEq, SmartDefault)]
+ pub enum Foo {
+ #[allow(dead_code)]
+ Bar,
+ #[default]
+ Baz,
+ #[allow(dead_code)]
+ Qux,
+ }
+
+ assert!(Foo::default() == Foo::Baz);
+}
+
+#[test]
+fn test_enum_of_tuples() {
+ #[derive(PartialEq, SmartDefault)]
+ pub enum Foo {
+ #[allow(dead_code)]
+ Bar(i32),
+ #[default]
+ Baz(#[default = 10] i32, i32),
+ #[allow(dead_code)]
+ Qux(i32),
+ }
+
+ assert!(Foo::default() == Foo::Baz(10, 0));
+}
+
+#[test]
+fn test_enum_of_structs() {
+ #[derive(PartialEq, SmartDefault)]
+ pub enum Foo {
+ #[allow(dead_code)]
+ Bar {
+ x: i32,
+ },
+ #[default]
+ Baz {
+ #[default = 10]
+ y: i32,
+ z: i32,
+ },
+ #[allow(dead_code)]
+ Qux {
+ w: i32,
+ },
+ }
+
+ assert!(Foo::default() == Foo::Baz { y: 10, z: 0 });
+}
+
+#[test]
+fn test_enum_mixed() {
+ #[derive(PartialEq, SmartDefault)]
+ enum Foo {
+ #[allow(dead_code)]
+ Bar,
+ #[default]
+ Baz(#[default = 10] i32),
+ #[allow(dead_code)]
+ Qux {
+ w: i32,
+ },
+ }
+
+ assert!(Foo::default() == Foo::Baz(10));
+}
+
+#[test]
+fn test_generics_type_parameters() {
+ #[derive(PartialEq, SmartDefault)]
+ struct Foo<T> where T: Default {
+ #[default(Some(Default::default()))]
+ x: Option<T>
+ }
+
+ assert!(Foo::default() == Foo { x: Some(0) });
+}
+
+#[test]
+fn test_generics_lifetime_parameters() {
+ // NOTE: A default value makes no sense with lifetime parameters, since ::default() receives no
+ // paramters and therefore can receive no lifetimes. But it does make sense if you make a variant
+ // without ref fields the default.
+
+ #[derive(PartialEq, SmartDefault)]
+ enum Foo<'a> {
+ #[default]
+ Bar(i32),
+ #[allow(dead_code)]
+ Baz(&'a str)
+ }
+
+ assert!(Foo::default() == Foo::Bar(0));
+}
+
+#[test]
+fn test_code_hack() {
+ #[derive(PartialEq, SmartDefault)]
+ struct Foo {
+ #[default(_code = "vec![1, 2, 3]")]
+ v: Vec<u32>,
+ }
+
+ assert!(Foo::default().v == [1, 2, 3]);
+}
+
+#[test]
+fn test_string_conversion() {
+ #[derive(PartialEq, SmartDefault)]
+ struct Foo(
+ #[default = "one"]
+ &'static str,
+ #[default("two")]
+ String,
+ );
+
+ assert!(Foo::default() == Foo("one", "two".to_owned()));
+}