• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1Remain sorted
2=============
3
4[![Build Status](https://api.travis-ci.com/dtolnay/remain.svg?branch=master)](https://travis-ci.com/dtolnay/remain)
5[![Latest Version](https://img.shields.io/crates/v/remain.svg)](https://crates.io/crates/remain)
6[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/remain)
7
8This crate provides an attribute macro to check at compile time that the
9variants of an enum or the arms of a match expression are written in sorted
10order.
11
12```toml
13[dependencies]
14remain = "0.1"
15```
16
17## Syntax
18
19Place a `#[remain::sorted]` attribute on enums, structs, match-expressions, or
20let-statements whose value is a match-expression.
21
22Alternatively, import as `use remain::sorted;` and use `#[sorted]` as the
23attribute.
24
25```rust
26#[remain::sorted]
27#[derive(Debug)]
28pub enum Error {
29    BlockSignal(signal::Error),
30    CreateCrasClient(libcras::Error),
31    CreateEventFd(sys_util::Error),
32    CreateSignalFd(sys_util::SignalFdError),
33    CreateSocket(io::Error),
34    DetectImageType(qcow::Error),
35    DeviceJail(io_jail::Error),
36    NetDeviceNew(virtio::NetError),
37    SpawnVcpu(io::Error),
38}
39
40#[remain::sorted]
41#[derive(Debug)]
42pub enum Registers {
43    ax: u16,
44    cx: u16,
45    di: u16,
46    si: u16,
47    sp: u16,
48}
49
50impl Display for Error {
51    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52        use self::Error::*;
53
54        #[remain::sorted]
55        match self {
56            BlockSignal(e) => write!(f, "failed to block signal: {}", e),
57            CreateCrasClient(e) => write!(f, "failed to create cras client: {}", e),
58            CreateEventFd(e) => write!(f, "failed to create eventfd: {}", e),
59            CreateSignalFd(e) => write!(f, "failed to create signalfd: {}", e),
60            CreateSocket(e) => write!(f, "failed to create socket: {}", e),
61            DetectImageType(e) => write!(f, "failed to detect disk image type: {}", e),
62            DeviceJail(e) => write!(f, "failed to jail device: {}", e),
63            NetDeviceNew(e) => write!(f, "failed to set up virtio networking: {}", e),
64            SpawnVcpu(e) => write!(f, "failed to spawn VCPU thread: {}", e),
65        }
66    }
67}
68```
69
70If an enum variant, struct field, or match arm is inserted out of order,
71
72```diff
73      NetDeviceNew(virtio::NetError),
74      SpawnVcpu(io::Error),
75+     AaaUhOh(Box<dyn StdError>),
76  }
77```
78
79then the macro produces a compile error.
80
81```console
82error: AaaUhOh should sort before BlockSignal
83  --> tests/stable.rs:49:5
84   |
8549 |     AaaUhOh(Box<dyn StdError>),
86   |     ^^^^^^^
87```
88
89## Compiler support
90
91The attribute on enums and structs is supported on any rustc version 1.31+.
92
93Rust does not yet have stable support for user-defined attributes within a
94function body, so the attribute on match-expressions and let-statements requires
95a nightly compiler and the following two features enabled:
96
97```rust
98#![feature(proc_macro_hygiene, stmt_expr_attributes)]
99```
100
101As a stable alternative, this crate provides a function-level attribute called
102`#[remain::check]` which makes match-expression and let-statement attributes
103work on any rustc version 1.31+. Place this attribute on any function containing
104`#[sorted]` to make them work on a stable compiler.
105
106```rust
107impl Display for Error {
108    #[remain::check]
109    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110        use self::Error::*;
111
112        #[sorted]
113        match self {
114            /* ... */
115        }
116    }
117}
118```
119
120<br>
121
122#### License
123
124<sup>
125Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
1262.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
127</sup>
128
129<br>
130
131<sub>
132Unless you explicitly state otherwise, any contribution intentionally submitted
133for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
134be dual licensed as above, without any additional terms or conditions.
135</sub>
136