Skip to main content

Crate opaque_enum

Crate opaque_enum 

Source
Expand description

Opaque enum support.

This crate exposes the opaque_enum attribute macro plus the projection trait used by generated forwarding impls. The macro lets a public type keep an enum-like authoring experience while exposing an opaque wrapper instead of public enum variants.

§Example

use opaque_enum::opaque_enum;
use std::fmt::{self, Display, Formatter};

#[opaque_enum]
#[derive(Debug)]
pub enum DatabaseError {
    ConnectionFailed(String),
    QueryFailed { query: String, reason: String },
    PermissionDenied,
}

#[opaque_enum]
impl Display for DatabaseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::ConnectionFailed(err) => write!(f, "connection failed: {err}"),
            Self::QueryFailed { query, reason } => {
                write!(f, "query `{query}` failed: {reason}")
            }
            Self::PermissionDenied => write!(f, "permission denied"),
        }
    }
}

let err = DatabaseError::ConnectionFailed("timeout".to_owned());
assert_eq!(err.to_string(), "connection failed: timeout");

Traits§

OpaqueProject
Projects an opaque wrapper receiver into the corresponding inner receiver.

Attribute Macros§

opaque_enum
Hides enum variants behind an opaque struct wrapper.