Commit graph

6 commits

Author SHA1 Message Date
David Tolnay
aecf9a4dee edition: Remove extern crate lines
In Rust 2018 edition, `extern crate` is no longer required for importing
from other crates. Instead of writing:

    extern crate dep;
    use dep::Thing;

we write:

    use dep::Thing;

In this approach, macros are imported individually from the declaring
crate rather than through #[macro_use]. Before:

    #[macro_use]
    extern crate sys_util;

After:

    use sys_util::{debug, error};

The only place that `extern crate` continues to be required is in
importing the compiler's proc_macro API into a procedural macro crate.
This will hopefully be fixed in a future Rust release.

    extern crate proc_macro;

TEST=cargo check
TEST=cargo check --all-features
TEST=cargo check --target aarch64-unknown-linux-gnu
TEST=local kokoro

Change-Id: I0b43768c0d81f2a250b1959fb97ba35cbac56293
Reviewed-on: https://chromium-review.googlesource.com/1565302
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-04-15 02:06:08 -07:00
David Tolnay
633426a8fc edition: Fill in macro imports
Macros were previously imported through `#[macro_use] extern crate`,
which is basically a glob import of all macros from the crate. As of
2018 edition of Rust, `extern crate` is no longer required and macros
are imported individually like any other item from a dependency. This CL
fills in all the appropriate macro imports that will allow us to remove
our use of `extern crate` in a subsequent CL.

TEST=cargo check --all-features --tests
TEST=kokoro

Change-Id: If2ec08b06b743abf5f62677c6a9927c3d5d90a54
Reviewed-on: https://chromium-review.googlesource.com/1565546
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-04-15 02:06:07 -07:00
Daniel Verkamp
7154c1d8ae Silence non_upper_case_globals warnings in macros
The enumn and bitfield macros generate global constants based on names
that are typically in CamelCase, but the new on-by-default warning
non_upper_case_globals complains about them.

Fixes warnings of the form:
  warning: associated constant `...` should have an upper case name
when using enumn or bitfield.

BUG=None
TEST=`cargo build` without warnings

Change-Id: Id908df1dcdf58288c2cbdff574cb70be2026bde6
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1536558
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-04-09 01:55:16 -07:00
David Tolnay
98895ac05d edition: Update enumn crate to 2018 edition
Separated out of CL:1513058 to make it possible to land parts
individually while the affected crate has no other significant CLs
pending. This avoids repeatedly introducing non-textual conflicts with
new code that adds `use` statements.

TEST=cargo check
TEST=cargo check --all-features
TEST=cargo check --target aarch64-unknown-linux-gnu

Change-Id: Ia446b796d9f6bf3ddf9813ee0678242697dd1f73
Reviewed-on: https://chromium-review.googlesource.com/1519694
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-04-08 02:51:37 -07:00
David Tolnay
fe3ef7d998 edition: Update absolute paths to 2018 style
This is an easy step toward adopting 2018 edition eventually, and will
make any future CL that sets `edition = "2018"` this much smaller.

The module system changes in Rust 2018 are described here:

https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-clarity.html

Generated by running:

    cargo fix --edition --all

in each workspace, followed by bin/fmt.

TEST=cargo check
TEST=cargo check --all-features
TEST=cargo check --target aarch64-unknown-linux-gnu

Change-Id: I000ab5e69d69aa222c272fae899464bbaf65f6d8
Reviewed-on: https://chromium-review.googlesource.com/1513054
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-03-13 21:05:03 -07:00
David Tolnay
f97991985d macros: Derive macro to generate integer to enum conversion
This CL adds a procedural macro to generate functions for converting a
primitive integer into the corresponding variant of an enum.

Loosely based on https://docs.rs/enum-primitive-derive but implemented
against a newer version of Syn and without the dependency on num-traits.

The generated function is named `n` and has the following signature:

    impl YourEnum {
        pub fn n(value: Repr) -> Option<Self>;
    }

where `Repr` is an integer type of the right size as described in more
detail below.

EXAMPLE

    extern crate enumn;

    #[derive(PartialEq, Debug, enumn::N)]
    enum Status {
        LegendaryTriumph,
        QualifiedSuccess,
        FortuitousRevival,
        IndeterminateStalemate,
        RecoverableSetback,
        DireMisadventure,
        AbjectFailure,
    }

    fn main() {
        let s = Status::n(1);
        assert_eq!(s, Some(Status::QualifiedSuccess));

        let s = Status::n(9);
        assert_eq!(s, None);
    }

SIGNATURE

The generated signature depends on whether the enum has a `#[repr(..)]`
attribute. If a `repr` is specified, the input to `n` will be required
to be of that type.

    #[derive(enumn::N)]
    #[repr(u8)]
    enum E {
        /* ... */
    }

    // expands to:
    impl E {
        pub fn n(value: u8) -> Option<Self> {
            /* ... */
        }
    }

On the other hand if no `repr` is specified then we get a signature that
is generic over a variety of possible types.

    impl E {
        pub fn n<REPR: Into<i64>>(value: REPR) -> Option<Self> {
            /* ... */
        }
    }

DISCRIMINANTS

The conversion respects explictly specified enum discriminants. Consider
this enum:

    #[derive(enumn::N)]
    enum Letter {
        A = 65,
        B = 66,
    }

Here `Letter::n(65)` would return `Some(Letter::A)`.

TEST=`cargo test` against the new crate

Change-Id: I4286a816828c83507b35185fe497455ee30ae9e8
Reviewed-on: https://chromium-review.googlesource.com/1365114
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2018-12-07 17:35:43 -08:00