Skip to content

Commit

Permalink
refac(elf): start on TryFrom impls for #85
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Mar 10, 2017
1 parent b2352d1 commit 514d792
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
22 changes: 17 additions & 5 deletions elf/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! ELF file header
use super::{ElfResult, ElfWord, Section, section};
use super::ValidatesWord;
use core::{fmt, mem};
use core::{fmt, mem, convert};

/// Trait representing an ELF File Header.
///
Expand All @@ -26,12 +26,10 @@ use core::{fmt, mem};
/// [HeaderRepr]: struct.HeaderRepr.html
/// [specification](http://www.sco.com/developers/gabi/latest/ch4.eheader.html)
/// [OS Dev Wiki](http://wiki.osdev.org/ELF#Header)
pub trait Header {
pub trait Header: Sized {
type Word: ElfWord;

/// Attempt to extract an ELF file header from a slice of bytes.
/// TODO: rewrite this as a `TryFrom` implementation (see issue #85)
// - eliza, 03/09/2017
fn from_slice<'a>(input: &'a [u8]) -> ElfResult<&'a Self>;

/// Attempt to extract a section header from a slice of bytes.
Expand Down Expand Up @@ -80,6 +78,8 @@ pub trait Header {

}



macro_rules! impl_getters {
($(#[$attr:meta])* pub fn $name:ident(&self) -> $ty:ident; $($rest:tt)*) => {
$(#[$attr])* #[inline] pub fn $name(&self) -> $ty { self.$name as $ty }
Expand Down Expand Up @@ -170,7 +170,17 @@ macro_rules! Header {
fn ident(&self) -> Ident;
fn machine(&self) -> Machine;
}
})+
}

impl<'a> convert::TryFrom<&'a [u8]> for $name<$size>
where $name<$size>: Header {
type Err = &'static str;
#[inline]
fn try_from(slice: &'a [u8]) -> ElfResult<Self> {
<Self as Header>::from_slice(slice).map(|x| *x)
}
}
)+
}
}

Expand Down Expand Up @@ -344,6 +354,8 @@ pub enum Version { None = 0, Current = 1 }
struct TypeRepr(u16);

impl TypeRepr {
/// TODO: rewrite this as a `convert::Into` implementation
/// - eliza, 03/09/2017
pub fn as_type(&self) -> Type {
match self.0 {
0 => Type::None
Expand Down
7 changes: 7 additions & 0 deletions elf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//!
//! [elfspec]: http://www.skyfree.org/linux/references/ELF_Format.pdf
#![feature(core_intrinsics)]
#![feature(try_from)]
#![no_std]

#[macro_use] extern crate bitflags;
Expand Down Expand Up @@ -91,6 +92,12 @@ where Word: ElfWord + 'a
// thanks to Max for making me figure this out.
/// TODO: rewrite this as a `TryFrom` implementation (see issue #85)
// - eliza, 03/09/2017
/// wait, possibly we should NOT do that. actually we should
/// almost certainly not do that. since this function is unsafe,
/// but `TryFrom` is not, and because this would be WAY generic.
// - eliza, 03/09/2017
/// TODO: is this general enough to move into util?
// - eliza, 03/09/2017
unsafe fn extract_from_slice<T: Sized>( data: &[u8]
, offset: usize
, n: usize)
Expand Down

0 comments on commit 514d792

Please sign in to comment.