#[opaque_enum]Expand description
Hides enum variants behind an opaque struct wrapper.
This macro lets a public type keep an enum-like authoring experience while exposing an opaque wrapper instead of public enum variants. This prevents breaking changes when you add, remove, or modify variants.
§Examples
ⓘ
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"),
}
}
}You can also opt-in to boxing the representation by specifying wrapper = Box:
ⓘ
#[opaque_enum(wrapper = Box)]
#[derive(Debug)]
pub enum LargeError {
Variant1([u8; 1024]),
Variant2,
}