Skip to content
Merged
85 changes: 58 additions & 27 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use crate::{Entry, HashMap, HashSet};
use std::borrow::Cow;
use std::cell::Cell;
use std::collections::VecDeque;
use std::fmt::Write;
use std::fmt::{self, Write};
use std::ops;
use std::str::FromStr;

Expand All @@ -73,13 +73,13 @@ impl From<std::io::Error> for CodegenError {
}
}

impl std::fmt::Display for CodegenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for CodegenError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CodegenError::Serialize { msg, loc } => {
Self::Serialize { msg, loc } => {
write!(f, "serialization error at {}: {}", loc, msg)
}
CodegenError::Io(err) => err.fmt(f),
Self::Io(err) => err.fmt(f),
}
}
}
Expand Down Expand Up @@ -2736,6 +2736,35 @@ impl Default for EnumVariation {
}
}

impl fmt::Display for EnumVariation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::Rust {
non_exhaustive: false,
} => "rust",
Self::Rust {
non_exhaustive: true,
} => "rust_non_exhaustive",
Self::NewType {
is_bitfield: true, ..
} => "bitfield",
Self::NewType {
is_bitfield: false,
is_global,
} => {
if *is_global {
"newtype_global"
} else {
"newtype"
}
}
Self::Consts => "consts",
Self::ModuleConsts => "moduleconsts",
};
s.fmt(f)
}
}

impl std::str::FromStr for EnumVariation {
type Err = std::io::Error;

Expand Down Expand Up @@ -3422,13 +3451,13 @@ pub enum MacroTypeVariation {
Unsigned,
}

impl MacroTypeVariation {
/// Convert a `MacroTypeVariation` to its str representation.
pub(crate) fn as_str(&self) -> &str {
match self {
MacroTypeVariation::Signed => "signed",
MacroTypeVariation::Unsigned => "unsigned",
}
impl fmt::Display for MacroTypeVariation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::Signed => "signed",
Self::Unsigned => "unsigned",
};
s.fmt(f)
}
}

Expand Down Expand Up @@ -3468,14 +3497,15 @@ pub enum AliasVariation {
NewTypeDeref,
}

impl AliasVariation {
/// Convert an `AliasVariation` to its str representation.
pub(crate) fn as_str(&self) -> &str {
match self {
AliasVariation::TypeAlias => "type_alias",
AliasVariation::NewType => "new_type",
AliasVariation::NewTypeDeref => "new_type_deref",
}
impl fmt::Display for AliasVariation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::TypeAlias => "type_alias",
Self::NewType => "new_type",
Self::NewTypeDeref => "new_type_deref",
};

s.fmt(f)
}
}

Expand Down Expand Up @@ -3505,10 +3535,10 @@ impl std::str::FromStr for AliasVariation {
}
}

/// Enum for how non-Copy unions should be translated.
/// Enum for how non-`Copy` `union`s should be translated.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum NonCopyUnionStyle {
/// Wrap members in a type generated by bindgen.
/// Wrap members in a type generated by `bindgen`.
BindgenWrapper,
/// Wrap members in [`::core::mem::ManuallyDrop`].
///
Expand All @@ -3517,13 +3547,14 @@ pub enum NonCopyUnionStyle {
ManuallyDrop,
}

impl NonCopyUnionStyle {
/// Convert an `NonCopyUnionStyle` to its str representation.
pub(crate) fn as_str(&self) -> &'static str {
match self {
impl fmt::Display for NonCopyUnionStyle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::BindgenWrapper => "bindgen_wrapper",
Self::ManuallyDrop => "manually_drop",
}
};

s.fmt(f)
}
}

Expand Down
Loading