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