Add default std feature to allow no_std use-cases

This addresses the ask in #19
This commit is contained in:
Christopher Cole 2022-10-20 23:00:57 -07:00
parent 9c5e4b62d6
commit 1be248c4bb
No known key found for this signature in database
GPG Key ID: 0AC856975983E9DB
4 changed files with 24 additions and 8 deletions

View File

@ -14,3 +14,7 @@ edition = "2021"
name = "elf" name = "elf"
[dependencies] [dependencies]
[features]
default = ["std"]
std = []

View File

@ -47,10 +47,9 @@ unsafe code work, but nevertheless it introduces unsafe code blocks (albeit
small ones). This crate strives to serve as an alternate implementation with small ones). This crate strives to serve as an alternate implementation with
zero unsafe code blocks. zero unsafe code blocks.
# Future plans ### no_std option:
Only disables the std:: Read + Seek interface and limits the library to the
**Add no_std option** This would disable the Read + Seek interface and limit `&[u8]` parsing impl. All other ELF parsing functionality is still available!
the library to the `&[u8]` parsing impl.
## Example: ## Example:
```rust ```rust

View File

@ -42,12 +42,13 @@
//! small ones). This crate strives to serve as an alternate implementation with //! small ones). This crate strives to serve as an alternate implementation with
//! zero unsafe code blocks. //! zero unsafe code blocks.
//! //!
//! # Future plans //! ### no_std option:
//! //! Only disables the std:: Read + Seek interface and limits the library to the
//! **Add no_std option** This would disable the Read + Seek interface and limit //! `&[u8]` parsing impl. All other ELF parsing functionality is still available!
//! the library to the `&[u8]` parsing impl.
//! //!
#![cfg_attr(not(feature = "std"), no_std)]
pub mod file; pub mod file;
pub mod gabi; pub mod gabi;
pub mod section; pub mod section;

View File

@ -1,6 +1,10 @@
use core::ops::Range; use core::ops::Range;
#[cfg(feature = "std")]
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
#[cfg(feature = "std")]
use std::collections::HashMap; use std::collections::HashMap;
#[cfg(feature = "std")]
use std::io::{Read, Seek, SeekFrom}; use std::io::{Read, Seek, SeekFrom};
#[derive(Debug)] #[derive(Debug)]
@ -40,11 +44,13 @@ pub enum ParseError {
/// Returned when parsing an ELF structure and the underlying structure data /// Returned when parsing an ELF structure and the underlying structure data
/// was truncated and thus the full structure contents could not be parsed. /// was truncated and thus the full structure contents could not be parsed.
TryFromSliceError(core::array::TryFromSliceError), TryFromSliceError(core::array::TryFromSliceError),
#[cfg(feature = "std")]
/// Returned when parsing an ELF structure out of an io stream encountered /// Returned when parsing an ELF structure out of an io stream encountered
/// an io error. /// an io error.
IOError(std::io::Error), IOError(std::io::Error),
} }
#[cfg(feature = "std")]
impl std::error::Error for ParseError { impl std::error::Error for ParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self { match *self {
@ -59,6 +65,7 @@ impl std::error::Error for ParseError {
ParseError::SliceReadError(_) => None, ParseError::SliceReadError(_) => None,
ParseError::Utf8Error(ref err) => Some(err), ParseError::Utf8Error(ref err) => Some(err),
ParseError::TryFromSliceError(ref err) => Some(err), ParseError::TryFromSliceError(ref err) => Some(err),
#[cfg(feature = "std")]
ParseError::IOError(ref err) => Some(err), ParseError::IOError(ref err) => Some(err),
} }
} }
@ -105,6 +112,7 @@ impl core::fmt::Display for ParseError {
} }
ParseError::Utf8Error(ref err) => err.fmt(f), ParseError::Utf8Error(ref err) => err.fmt(f),
ParseError::TryFromSliceError(ref err) => err.fmt(f), ParseError::TryFromSliceError(ref err) => err.fmt(f),
#[cfg(feature = "std")]
ParseError::IOError(ref err) => err.fmt(f), ParseError::IOError(ref err) => err.fmt(f),
} }
} }
@ -122,6 +130,7 @@ impl From<core::array::TryFromSliceError> for ParseError {
} }
} }
#[cfg(feature = "std")]
impl From<std::io::Error> for ParseError { impl From<std::io::Error> for ParseError {
fn from(err: std::io::Error) -> ParseError { fn from(err: std::io::Error) -> ParseError {
ParseError::IOError(err) ParseError::IOError(err)
@ -218,11 +227,13 @@ impl ReadBytesAt for &[u8] {
} }
} }
#[cfg(feature = "std")]
pub struct CachedReadBytes<R: Read + Seek> { pub struct CachedReadBytes<R: Read + Seek> {
reader: R, reader: R,
bufs: HashMap<(u64, u64), Box<[u8]>>, bufs: HashMap<(u64, u64), Box<[u8]>>,
} }
#[cfg(feature = "std")]
impl<R: Read + Seek> CachedReadBytes<R> { impl<R: Read + Seek> CachedReadBytes<R> {
pub fn new(reader: R) -> Self { pub fn new(reader: R) -> Self {
CachedReadBytes { CachedReadBytes {
@ -232,6 +243,7 @@ impl<R: Read + Seek> CachedReadBytes<R> {
} }
} }
#[cfg(feature = "std")]
impl<R: Read + Seek> ReadBytesAt for &mut CachedReadBytes<R> { impl<R: Read + Seek> ReadBytesAt for &mut CachedReadBytes<R> {
fn read_bytes_at(&mut self, range: Range<usize>) -> Result<&[u8], ParseError> { fn read_bytes_at(&mut self, range: Range<usize>) -> Result<&[u8], ParseError> {
if range.len() == 0 { if range.len() == 0 {