Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Use Rust's #[non_exhaustive] lint for enums #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/colortype.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::NonExhaustiveMarker;

/// An enumeration over supported color types and bit depths
#[non_exhaustive]
#[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
pub enum ColorType {
/// Pixel is 8-bit luminance
Expand All @@ -25,9 +24,6 @@ pub enum ColorType {
Bgr8,
/// Pixel is 8-bit BGR with an alpha channel
Bgra8,

#[doc(hidden)]
__Nonexhaustive(NonExhaustiveMarker),
}

impl ColorType {
Expand Down Expand Up @@ -66,6 +62,7 @@ impl ColorType {
/// Another purpose is to advise users of a rough estimate of the accuracy and effort of the
/// decoding from and encoding to such an image format.
#[allow(missing_docs)]
#[non_exhaustive]
#[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
pub enum ExtendedColorType {
L1,
Expand Down Expand Up @@ -95,9 +92,6 @@ pub enum ExtendedColorType {
/// which are associated with an external palette. In that case, the pixel value is an index
/// into the palette.
Unknown(u8),

#[doc(hidden)]
__Nonexhaustive(NonExhaustiveMarker),
}

impl ExtendedColorType {
Expand Down
16 changes: 4 additions & 12 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use std::{fmt, io};

use crate::ExtendedColorType;
use crate::ImageFormat;
use crate::NonExhaustiveMarker;

/// The generic error type for image operations.
///
Expand Down Expand Up @@ -77,6 +76,7 @@ pub struct UnsupportedError {
}

/// Details what feature is not supported.
#[non_exhaustive]
#[derive(Clone, Debug, Hash, PartialEq)]
pub enum UnsupportedErrorKind {
/// The required color type can not be handled.
Expand All @@ -86,8 +86,6 @@ pub enum UnsupportedErrorKind {
/// Some feature specified by string.
/// This is discouraged and is likely to get deprecated (but not removed).
GenericFeature(String),
#[doc(hidden)]
__NonExhaustive(NonExhaustiveMarker),
}

/// An error was encountered while encoding an image.
Expand Down Expand Up @@ -115,6 +113,7 @@ pub struct ParameterError {
}

/// Details how a parameter is malformed.
#[non_exhaustive]
#[derive(Clone, Debug, Hash, PartialEq)]
pub enum ParameterErrorKind {
/// The dimensions passed are wrong.
Expand All @@ -126,9 +125,6 @@ pub enum ParameterErrorKind {
Generic(String),
/// The end of the image has been reached.
NoMoreData,
#[doc(hidden)]
/// Do not use this, not part of stability guarantees.
__NonExhaustive(NonExhaustiveMarker),
}

/// An error was encountered while decoding an image.
Expand Down Expand Up @@ -162,18 +158,17 @@ pub struct LimitError {
/// detailed information or to incorporate other resources types.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
#[allow(missing_copy_implementations)] // Might be non-Copy in the future.
#[non_exhaustive]
pub enum LimitErrorKind {
/// The resulting image exceed dimension limits in either direction.
DimensionError,
/// The operation would have performed an allocation larger than allowed.
InsufficientMemory,
#[doc(hidden)]
/// Do not use this, not part of stability guarantees.
__NonExhaustive(NonExhaustiveMarker),
}

/// A best effort representation for image formats.
#[derive(Clone, Debug, Hash, PartialEq)]
#[non_exhaustive]
pub enum ImageFormatHint {
/// The format is known exactly.
Exact(ImageFormat),
Expand All @@ -186,9 +181,6 @@ pub enum ImageFormatHint {

/// The format is not known or could not be determined.
Unknown,

#[doc(hidden)]
__NonExhaustive(NonExhaustiveMarker),
}

impl UnsupportedError {
Expand Down
6 changes: 1 addition & 5 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::NonExhaustiveMarker;

/// An enumeration of supported image formats.
/// Not all formats support both encoding and decoding.
#[non_exhaustive]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum ImageFormat {
/// An Image in PNG Format
Expand Down Expand Up @@ -36,7 +35,4 @@ pub enum ImageFormat {

/// An Image in Radiance HDR Format
Hdr,

#[doc(hidden)]
__NonExhaustive(NonExhaustiveMarker),
}
16 changes: 0 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,5 @@ pub use decoder::*;
pub use error::*;
pub use format::ImageFormat;

/// A marker struct for __NonExhaustive enums.
///
/// This is an empty type that can not be constructed. When an enum contains a tuple variant that
/// includes this type the optimizer can statically determined tha the branch is never taken while
/// at the same time the matching of the branch is required.
///
/// The effect is thus very similar to the actual `#[non_exhaustive]` attribute with no runtime
/// costs. Also note that we use a dirty trick to not only hide this type from the doc but make it
/// inaccessible. The visibility in this module is pub but the module itself is not and the
/// top-level crate never exports the type.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct NonExhaustiveMarker {
/// Allows this crate, and this crate only, to match on the impossibility of this variant.
pub(crate) _private: Empty,
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Empty {}