Skip to content

Commit

Permalink
Merge pull request gimli-rs#306 from philipc/nonexhaustive
Browse files Browse the repository at this point in the history
Add `non_exhaustive` to all public enums that may be extended
  • Loading branch information
philipc authored May 27, 2021
2 parents 0e17af7 + 511face commit 1e5ac4c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 19 deletions.
12 changes: 2 additions & 10 deletions examples/nm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,20 @@ fn print_symbol(symbol: &Symbol<'_, '_>, section_kinds: &HashMap<SectionIndex, S
}

let mut kind = match symbol.section() {
SymbolSection::Unknown | SymbolSection::None => '?',
SymbolSection::Undefined => 'U',
SymbolSection::Absolute => 'A',
SymbolSection::Common => 'C',
SymbolSection::Section(index) => match section_kinds.get(&index) {
None
| Some(SectionKind::Unknown)
| Some(SectionKind::Other)
| Some(SectionKind::OtherString)
| Some(SectionKind::Debug)
| Some(SectionKind::Linker)
| Some(SectionKind::Note)
| Some(SectionKind::Metadata)
| Some(SectionKind::Elf(_)) => '?',
Some(SectionKind::Text) => 't',
Some(SectionKind::Data) | Some(SectionKind::Tls) | Some(SectionKind::TlsVariables) => {
'd'
}
Some(SectionKind::ReadOnlyData) | Some(SectionKind::ReadOnlyString) => 'r',
Some(SectionKind::UninitializedData) | Some(SectionKind::UninitializedTls) => 'b',
Some(SectionKind::Common) => 'C',
_ => '?',
},
_ => '?',
};

if symbol.is_global() {
Expand Down
5 changes: 3 additions & 2 deletions examples/objcopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ fn main() {
continue;
}
let (section, value) = match in_symbol.section() {
SymbolSection::Unknown => panic!("unknown symbol section for {:?}", in_symbol),
SymbolSection::None => (write::SymbolSection::None, in_symbol.address()),
SymbolSection::Undefined => (write::SymbolSection::Undefined, in_symbol.address()),
SymbolSection::Absolute => (write::SymbolSection::Absolute, in_symbol.address()),
Expand All @@ -95,6 +94,7 @@ fn main() {
continue;
}
}
_ => panic!("unknown symbol section for {:?}", in_symbol),
};
let flags = match in_symbol.flags() {
SymbolFlags::None => SymbolFlags::None,
Expand All @@ -111,6 +111,7 @@ fn main() {
associative_section,
}
}
_ => panic!("unknown symbol flags for {:?}", in_symbol),
};
let out_symbol = write::Symbol {
name: in_symbol.name().unwrap_or("").as_bytes().to_vec(),
Expand All @@ -133,11 +134,11 @@ fn main() {
let out_section = *out_sections.get(&in_section.index()).unwrap();
for (offset, in_relocation) in in_section.relocations() {
let symbol = match in_relocation.target() {
RelocationTarget::Absolute => unimplemented!(),
RelocationTarget::Symbol(symbol) => *out_symbols.get(&symbol).unwrap(),
RelocationTarget::Section(section) => {
out_object.section_symbol(*out_sections.get(&section).unwrap())
}
_ => panic!("unknown relocation target for {:?}", in_relocation),
};
let out_relocation = write::Relocation {
offset,
Expand Down
2 changes: 1 addition & 1 deletion examples/readobj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3695,7 +3695,7 @@ mod macho {
p.field_hex("Reserved", x.reserved.get(endian));
});
}
LoadCommandVariant::Other => {
_ => {
p.group("LoadCommand", |p| {
p.field_enum("Cmd", command.cmd(), FLAGS_LC);
p.field_hex("CmdSize", command.cmdsize());
Expand Down
23 changes: 17 additions & 6 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/// A CPU architecture.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Architecture {
Unknown,
Aarch64,
Arm,
I386,
Mips,
Mips64,
S390x,
Wasm32,
PowerPc,
PowerPc64,
Riscv32,
Riscv64,
S390x,
Wasm32,
X86_64,
}

Expand All @@ -29,13 +30,13 @@ impl Architecture {
Architecture::I386 => Some(AddressSize::U32),
Architecture::Mips => Some(AddressSize::U32),
Architecture::Mips64 => Some(AddressSize::U64),
Architecture::S390x => Some(AddressSize::U64),
Architecture::Wasm32 => Some(AddressSize::U32),
Architecture::X86_64 => Some(AddressSize::U64),
Architecture::PowerPc => Some(AddressSize::U32),
Architecture::PowerPc64 => Some(AddressSize::U64),
Architecture::Riscv64 => Some(AddressSize::U64),
Architecture::Riscv32 => Some(AddressSize::U32),
Architecture::Riscv64 => Some(AddressSize::U64),
Architecture::S390x => Some(AddressSize::U64),
Architecture::Wasm32 => Some(AddressSize::U32),
Architecture::X86_64 => Some(AddressSize::U64),
}
}
}
Expand All @@ -45,6 +46,7 @@ impl Architecture {
/// This may differ from the address size supported by the file format (such as for COFF).
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[repr(u8)]
pub enum AddressSize {
U32 = 4,
Expand All @@ -62,6 +64,7 @@ impl AddressSize {
/// A binary file format.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BinaryFormat {
Coff,
Elf,
Expand All @@ -72,6 +75,7 @@ pub enum BinaryFormat {

/// The kind of a section.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SectionKind {
/// The section kind is unknown.
Unknown,
Expand Down Expand Up @@ -170,6 +174,7 @@ impl SectionKind {
/// This determines the way in which the linker resolves multiple definitions of the COMDAT
/// sections.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ComdatKind {
/// The selection kind is unknown.
Unknown,
Expand Down Expand Up @@ -201,6 +206,7 @@ pub enum ComdatKind {

/// The kind of a symbol.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SymbolKind {
/// The symbol kind is unknown.
Unknown,
Expand Down Expand Up @@ -249,6 +255,7 @@ pub enum SymbolScope {
///
/// 'XxxRelative' means 'Xxx + A - P'. 'XxxOffset' means 'S + A - Xxx'.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RelocationKind {
/// S + A
Absolute,
Expand Down Expand Up @@ -288,6 +295,7 @@ pub enum RelocationKind {
/// This is usually architecture specific, such as specifying an addressing mode or
/// a specific instruction.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RelocationEncoding {
/// Generic encoding.
Generic,
Expand Down Expand Up @@ -317,6 +325,7 @@ pub enum RelocationEncoding {

/// File flags that are specific to each file format.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FileFlags {
/// No file flags.
None,
Expand All @@ -339,6 +348,7 @@ pub enum FileFlags {

/// Section flags that are specific to each file format.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SectionFlags {
/// No section flags.
None,
Expand All @@ -361,6 +371,7 @@ pub enum SectionFlags {

/// Symbol flags that are specific to each file format.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SymbolFlags<Section> {
/// No symbol flags.
None,
Expand Down
1 change: 1 addition & 0 deletions src/read/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::read::{self, Error, ReadError, ReadRef};
/// The kind of archive format.
// TODO: Gnu64 and Darwin64 (and Darwin for writing)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ArchiveKind {
/// There are no special files that indicate the archive format.
Unknown,
Expand Down
1 change: 1 addition & 0 deletions src/read/macho/load_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ impl<'data, E: Endian> LoadCommandData<'data, E> {

/// A `LoadCommand` that has been interpreted according to its `cmd` field.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum LoadCommandVariant<'data, E: Endian> {
/// `LC_SEGMENT`
Segment32(&'data macho::SegmentCommand32<E>, &'data [u8]),
Expand Down
3 changes: 3 additions & 0 deletions src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ pub struct SymbolIndex(pub usize);

/// The section where a symbol is defined.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SymbolSection {
/// The section is unknown.
Unknown,
Expand Down Expand Up @@ -489,6 +490,7 @@ impl<'data> CodeView<'data> {

/// The target referenced by a relocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RelocationTarget {
/// The target is a symbol.
Symbol(SymbolIndex),
Expand Down Expand Up @@ -558,6 +560,7 @@ impl Relocation {

/// A data compression format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CompressionFormat {
/// The data is uncompressed.
None,
Expand Down
4 changes: 4 additions & 0 deletions src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ impl Object {
/// A standard segment kind.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum StandardSegment {
Text,
Data,
Expand All @@ -554,6 +555,7 @@ pub enum StandardSegment {
/// A standard section kind.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum StandardSection {
Text,
Data,
Expand Down Expand Up @@ -696,6 +698,7 @@ impl Section {

/// The section where a symbol is defined.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SymbolSection {
/// The section is not applicable for this symbol (such as file symbols).
None,
Expand Down Expand Up @@ -822,6 +825,7 @@ pub struct Comdat {

/// The symbol name mangling scheme.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Mangling {
/// No symbol mangling.
None,
Expand Down

0 comments on commit 1e5ac4c

Please sign in to comment.