Commit graph

15 commits

Author SHA1 Message Date
David Tolnay
48ff4165d2 cargo: Sort all dependency lists in Cargo.toml
This may help reduce cases of conflicts between independent CLs each
appending a dependency at the bottom of the list, of which I hit two
today rebasing some of my open CLs.

TEST=cargo check --all-features

Change-Id: Ief10bb004cc7b44b107dc3841ce36c6b23632aed
Reviewed-on: https://chromium-review.googlesource.com/1557172
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
2019-04-09 01:55:14 -07:00
David Tolnay
cbe9be0b20 edition: Update bit_field and bit_field_derive 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: Ie62d28d63257e59e6bee68b5c42a1f4cf7b47bed
Reviewed-on: https://chromium-review.googlesource.com/1519690
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-04-07 23:24:29 -07:00
Jingkui Wang
1612ff7a83 improve bitfield type safety by allowing tuple struct field
user-defined tuple struct could be used to improve type safety.

TEST=cargo test
BUG=None

Change-Id: I8ce10fc51b79c277ab23029513b707f3dd621af5
Reviewed-on: https://chromium-review.googlesource.com/1546432
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-04-03 18:13:52 -07:00
Jingkui Wang
85fa41f7b2 implement bitfield for enum with a width
If we know the width of an enum type, we don't need 'power of 2' number
of variants.

BUG=None
TEST=cargo test

Change-Id: I8148b28f86bb8e4fd4f67d8a6382fc713dad1439
Reviewed-on: https://chromium-review.googlesource.com/1530455
Commit-Ready: Jingkui Wang <jkwang@google.com>
Tested-by: Jingkui Wang <jkwang@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-03-28 19:04:03 -07:00
Jingkui Wang
ca224cd547 Change default field type to setter/getter types and remove generated
tests

Setter/Getter types will allow better enum fields.
Generated tests should be tests for bit_field cargo rather than the
defined bit_field struct.

BUG=None
TEST=cargo test

Change-Id: Ib3594e35e28fc393d49c476c9c83fc632cac3190
Reviewed-on: https://chromium-review.googlesource.com/1530454
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Jingkui Wang <jkwang@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-03-28 11:17:29 -07:00
David Tolnay
c324429b46 bitfield: Support BitFieldSpecifier for enums
Previously, the getter and setter functions generated for a bitfield
struct by #[bitfield] all operated on primitive types like bool, u8, u16
etc.

This CL adds support for getters and setters defined in terms of
user-defined enums.

We make an enum bitfield-compatible by adding #[bitfield]. The number of
variants must be a power of 2.

    #[bitfield]
    enum TwoBits {
        Zero = 0b00,
        One = 0b01,
        Two = 0b10,
        Three = 0b11,
    }

And then it may be used to specify a field in a bitfield struct.

    #[bitfield]
    struct Struct {
        prefix: BitField1,
        two_bits: TwoBits,
        suffix: BitField5,
    }

The generated getters and setters for this struct would have the
following signatures:

    impl Struct {
        fn get_prefix(&self) -> u8;
        fn set_prefix(&mut self, val: u8);

        fn get_two_bits(&self) -> TwoBits;
        fn set_two_bits(&mut self, val: TwoBits);

        fn get_suffix(&self) -> u8;
        fn set_suffix(&mut self, val: u8);
    }

TEST=`cargo test` the bit_field and bit_field_derive crates
TEST=`cargo check` crosvm

Change-Id: Ibc8923e2877fda6ae8da5767731edcb68721a434
Reviewed-on: https://chromium-review.googlesource.com/1519686
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
2019-03-17 14:38:45 -07:00
Miriam Zimmerman
a446434855 Run cargo fmt
BUG=None
TEST=None

Change-Id: I6db08c2ccfc616e4e34ad0219580c084dda34675
Reviewed-on: https://chromium-review.googlesource.com/1475058
Commit-Ready: Miriam Zimmerman <mutexlox@chromium.org>
Tested-by: Miriam Zimmerman <mutexlox@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2019-02-16 04:14:53 -08:00
David Tolnay
f71764228a bitfield: Documentation and simplifications
Changes in this CL:

- Crate-level documentation for bit_field crate!

- Use absolute paths within generated code so that the caller is no
  longer required to have various implementation details from the
  bit_field crate in scope.

- Check that the total number of bits is a multiple of 8. Previously, it
  would generate compilable code that panicked when invoking accessors.

- Provide B0 .. B64 as shorthand for BitField0 .. BitField64.

- Use `bool` as the bool specifier rather than BitFieldBool.

- Disallow BitFieldSpecifier impls outside the bit_field crate.

- Simplify declaration of the BitFieldN types by replacing the recursive
  macro_rules with a simpler procedural macro.

TEST=`cargo test` in bit_field and in bit_field_derive

Change-Id: Ica9347bc89901de85f74366edd038fb5d8042ee6
Reviewed-on: https://chromium-review.googlesource.com/1382578
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Jingkui Wang <jkwang@google.com>
2019-01-03 15:04:22 -08:00
Jingkui Wang
13b8c090bb bitfield: update bitfield structs and support bool
BitFieldSpecifiers are now generated by macros.
Can use BitFieldBool to specify a bool field.

BUG=chromium:831850
TEST=local cargo build/test

Change-Id: Id6b4a773ab612cea39ba811c3ec1da212b618ba2
Reviewed-on: https://chromium-review.googlesource.com/1356912
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2018-12-19 01:12:51 -08:00
Daniel Verkamp
3397126b3c bitfield: reformat with cargo fmt
BUG=None
TEST=cargo fmt --all -- --check

Change-Id: Ieccf6d77af27d299a08d22d9b3ee4b44bd90c5b8
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1370625
Reviewed-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
2018-12-12 13:55:51 -08:00
David Tolnay
caf32ee5bb macros: Clean up bitfield macro by replacing the input item
Before:

    #[derive(BitField)]
    #[passthrough(derive(Clone, Copy, PartialEq))]
    pub struct TrbSchema {
        parameter: B64,
        status: B32,
        cycle: B1,
        flags: B9,
        trb_type: B6,
        control: B16,
    }

After:

    #[bitfield]
    #[derive(Clone, Copy, PartialEq)]
    pub struct Trb {
        parameter: B64,
        status: B32,
        cycle: B1,
        flags: B9,
        trb_type: B6,
        control: B16,
    }

This change eliminates the need for the `passthrough` attribute, and
avoids the separate `FooSchema` struct continuing to float around and
disrupt IDE autocomplete.

TEST=`cargo test` the bit_field_derive crate
TEST=`cargo check` the devices crate against a migrated CL:1144264

Change-Id: I950ce896607468c73852aa181827f1a5dc0f0227
Reviewed-on: https://chromium-review.googlesource.com/1366539
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Jingkui Wang <jkwang@google.com>
2018-12-09 00:48:19 -08:00
David Tolnay
d32efefb7a macros: Format code inside of quote! invocations
Rustfmt currently does not touch the content of macro invocations. Also
it used to have a bug where if it changed indentation of a block of code
containing a multi-line macro invocation then the macro input would not
get correspondingly indented. That bug was visible across some of the
code here.

For example:

    // rustfmt decides to un-indent the surrounding block:
        let data_size_in_bytes = quote!(
            ( #( #field_types::FIELD_WIDTH as usize )+* ) / 8
        );

    // poorly formatted resulting code:
    let data_size_in_bytes = quote!(
            ( #( #field_types::FIELD_WIDTH as usize )+* ) / 8
        );

    // should have been:
    let data_size_in_bytes = quote!(
        ( #( #field_types::FIELD_WIDTH as usize )+* ) / 8
    );

TEST=cargo check crosvm
TEST=cargo test each of the three proc-macro crates
CQ-DEPEND=CL:1338507

Change-Id: Id2d456a8d85d719fbc0a65624f153f0f9df6f500
Reviewed-on: https://chromium-review.googlesource.com/1338508
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2018-11-21 05:52:46 -08:00
David Tolnay
1d0b445be2 macros: Update syn to 0.15
This brings us onto the stable API surface area for procedural macros
that stabilized in Rust 1.30, rather than the string-based shim on
older compilers.

    https://blog.rust-lang.org/2018/10/25/Rust-1.30.0.html

Intervening release notes:

- https://github.com/dtolnay/syn/releases/tag/0.13.0
- https://github.com/dtolnay/syn/releases/tag/0.14.0
- https://github.com/dtolnay/syn/releases/tag/0.15.0

TEST=cargo check crosvm
TEST=cargo test each of the three proc-macro crates
TEST=build_packages
CQ-DEPEND=CL:1340766

Change-Id: Idcf14df0225ab41423b9a8639d0bba0a63513712
Reviewed-on: https://chromium-review.googlesource.com/1338507
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
2018-11-21 05:52:45 -08:00
Jingkui Wang
f226e28632 bitfield: Add fmt::Debug
implements fmt::Debug for bitfield

BUG=chromium:831850
TEST=cargo test

Change-Id: I58c2211a50726aaea0ea45164ae07c7b38eddea7
Reviewed-on: https://chromium-review.googlesource.com/1147111
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Jingkui Wang <jkwang@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
2018-08-07 01:51:08 -07:00
Jingkui Wang
8cad751ce0 crosvm: Add bit_field_derive
Support macro derive(BitField) to make life easier.

BUG=None.
TEST=local build and run test.

Change-Id: I582620de250017fb7c0b601f9ad4fbcbbc2fe02a
Reviewed-on: https://chromium-review.googlesource.com/1069331
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Jingkui Wang <jkwang@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
2018-08-03 22:14:45 -07:00