]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #94530 - tmiasko:alignment-impls, r=dtolnay
authorbors <bors@rust-lang.org>
Sat, 21 May 2022 19:49:51 +0000 (19:49 +0000)
committerbors <bors@rust-lang.org>
Sat, 21 May 2022 19:49:51 +0000 (19:49 +0000)
Implement Copy, Clone, PartialEq and Eq for core::fmt::Alignment

Alignment is a fieldless exhaustive enum, so it is already possible to
clone and compare it by matching, but it is inconvenient to do so. For
example, if one would like to create a struct describing a formatter
configuration and provide a clone implementation:

```rust
pub struct Format {
    fill: char,
    width: Option<usize>,
    align: fmt::Alignment,
}

impl Clone for Format {
    fn clone(&self) -> Self {
        Format {
            align: match self.align {
                fmt::Alignment::Left => fmt::Alignment::Left,
                fmt::Alignment::Right => fmt::Alignment::Right,
                fmt::Alignment::Center => fmt::Alignment::Center,
            },
            .. *self
        }
    }
}
```

Derive Copy, Clone, PartialEq, and Eq for Alignment for convenience.

library/core/src/fmt/mod.rs

index 700a78cac5ab8abf28a5b088fe517646a3611314..dde9bc383d23c27cc45886a35b6c42cd39c19b36 100644 (file)
@@ -21,7 +21,7 @@
 #[stable(feature = "fmt_flags_align", since = "1.28.0")]
 #[cfg_attr(not(test), rustc_diagnostic_item = "Alignment")]
 /// Possible alignments returned by `Formatter::align`
-#[derive(Debug)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 pub enum Alignment {
     #[stable(feature = "fmt_flags_align", since = "1.28.0")]
     /// Indication that contents should be left-aligned.