Struct gimli::read::EndianReader [−][src]
An easy way to define a custom Reader
implementation with a reference to a
generic buffer of bytes and an associated endianity.
Note that the whole original buffer is kept alive in memory even if there is
only one reader that references only a handful of bytes from that original
buffer. That is, EndianReader
will not do any copying, moving, or
compacting in order to free up unused regions of the original buffer. If you
require this kind of behavior, it is up to you to implement Reader
directly by-hand.
Example
Say you have an mmap
ed file that you want to serve as a gimli::Reader
.
You can wrap that mmap
ed file up in a MmapFile
type and use
EndianReader<Rc<MmapFile>>
or EndianReader<Arc<MmapFile>>
as readers as
long as MmapFile
dereferences to the underlying [u8]
data.
use std::io; use std::ops::Deref; use std::path::Path; use std::slice; use std::sync::Arc; /// A type that represents an `mmap`ed file. #[derive(Debug)] pub struct MmapFile { ptr: *const u8, len: usize, } impl MmapFile { pub fn new(path: &Path) -> io::Result<MmapFile> { // Call `mmap` and check for errors and all that... } } impl Drop for MmapFile { fn drop(&mut self) { // Call `munmap` to clean up after ourselves... } } // And `MmapFile` can deref to a slice of the `mmap`ed region of memory. impl Deref for MmapFile { type Target = [u8]; fn deref(&self) -> &[u8] { unsafe { slice::from_raw_parts(self.ptr, self.len) } } } /// A type that represents a shared `mmap`ed file. #[derive(Debug, Clone)] pub struct ArcMmapFile(Arc<MmapFile>); // And `ArcMmapFile` can deref to a slice of the `mmap`ed region of memory. impl Deref for ArcMmapFile { type Target = [u8]; fn deref(&self) -> &[u8] { &self.0 } } // These are both valid for any `Rc` or `Arc`. unsafe impl gimli::StableDeref for ArcMmapFile {} unsafe impl gimli::CloneStableDeref for ArcMmapFile {} /// A `gimli::Reader` that is backed by an `mmap`ed file! pub type MmapFileReader<Endian> = gimli::EndianReader<Endian, ArcMmapFile>;
Implementations
impl<Endian, T> EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
pub fn new(bytes: T, endian: Endian) -> EndianReader<Endian, T>
[src]
Construct a new EndianReader
with the given bytes.
pub fn bytes(&self) -> &[u8]
[src]
Return a reference to the raw bytes underlying this reader.
impl<Endian, T> EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
Range Methods
Unfortunately, std::ops::Index
must return a reference, so we can’t
implement Index<Range<usize>>
to return a new EndianReader
the way we
would like to. Instead, we abandon fancy indexing operators and have these
plain old methods.
pub fn range(&self, idx: Range<usize>) -> EndianReader<Endian, T>
[src]
Take the given start..end
range of the underlying buffer and return a
new EndianReader
.
use gimli::{EndianReader, LittleEndian}; use std::sync::Arc; let buf = Arc::<[u8]>::from(&[0x01, 0x02, 0x03, 0x04][..]); let reader = EndianReader::new(buf.clone(), LittleEndian); assert_eq!(reader.range(1..3), EndianReader::new(&buf[1..3], LittleEndian));
Panics
Panics if the range is out of bounds.
pub fn range_from(&self, idx: RangeFrom<usize>) -> EndianReader<Endian, T>
[src]
Take the given start..
range of the underlying buffer and return a new
EndianReader
.
use gimli::{EndianReader, LittleEndian}; use std::sync::Arc; let buf = Arc::<[u8]>::from(&[0x01, 0x02, 0x03, 0x04][..]); let reader = EndianReader::new(buf.clone(), LittleEndian); assert_eq!(reader.range_from(2..), EndianReader::new(&buf[2..], LittleEndian));
Panics
Panics if the range is out of bounds.
pub fn range_to(&self, idx: RangeTo<usize>) -> EndianReader<Endian, T>
[src]
Take the given ..end
range of the underlying buffer and return a new
EndianReader
.
use gimli::{EndianReader, LittleEndian}; use std::sync::Arc; let buf = Arc::<[u8]>::from(&[0x01, 0x02, 0x03, 0x04][..]); let reader = EndianReader::new(buf.clone(), LittleEndian); assert_eq!(reader.range_to(..3), EndianReader::new(&buf[..3], LittleEndian));
Panics
Panics if the range is out of bounds.
Trait Implementations
impl<Endian: Clone, T: Clone> Clone for EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
fn clone(&self) -> EndianReader<Endian, T>
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<Endian: Copy, T: Copy> Copy for EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
impl<Endian: Debug, T: Debug> Debug for EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
impl<Endian, T> Deref for EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
impl<Endian, T> Eq for EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
impl<Endian: Hash, T: Hash> Hash for EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<Endian, T> Index<RangeFrom<usize>> for EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
type Output = [u8]
The returned type after indexing.
fn index(&self, idx: RangeFrom<usize>) -> &Self::Output
[src]
impl<Endian, T> Index<usize> for EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
impl<Endian, T1, T2> PartialEq<EndianReader<Endian, T2>> for EndianReader<Endian, T1> where
Endian: Endianity,
T1: CloneStableDeref<Target = [u8]> + Debug,
T2: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T1: CloneStableDeref<Target = [u8]> + Debug,
T2: CloneStableDeref<Target = [u8]> + Debug,
fn eq(&self, rhs: &EndianReader<Endian, T2>) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<Endian, T> Reader for EndianReader<Endian, T> where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
[src]
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
type Endian = Endian
The endianity of bytes that are read.
type Offset = usize
The type used for offsets and lengths.
fn endian(&self) -> Endian
[src]
fn len(&self) -> usize
[src]
fn empty(&mut self)
[src]
fn truncate(&mut self, len: usize) -> Result<()>
[src]
fn offset_from(&self, base: &EndianReader<Endian, T>) -> usize
[src]
fn offset_id(&self) -> ReaderOffsetId
[src]
fn lookup_offset_id(&self, id: ReaderOffsetId) -> Option<Self::Offset>
[src]
fn find(&self, byte: u8) -> Result<usize>
[src]
fn skip(&mut self, len: usize) -> Result<()>
[src]
fn split(&mut self, len: usize) -> Result<Self>
[src]
fn to_slice(&self) -> Result<Cow<'_, [u8]>>
[src]
fn to_string(&self) -> Result<Cow<'_, str>>
[src]
fn to_string_lossy(&self) -> Result<Cow<'_, str>>
[src]
fn read_slice(&mut self, buf: &mut [u8]) -> Result<()>
[src]
fn read_u8_array<A>(&mut self) -> Result<A> where
A: Sized + Default + AsMut<[u8]>,
[src]
A: Sized + Default + AsMut<[u8]>,
fn is_empty(&self) -> bool
[src]
fn read_u8(&mut self) -> Result<u8>
[src]
fn read_i8(&mut self) -> Result<i8>
[src]
fn read_u16(&mut self) -> Result<u16>
[src]
fn read_i16(&mut self) -> Result<i16>
[src]
fn read_u32(&mut self) -> Result<u32>
[src]
fn read_i32(&mut self) -> Result<i32>
[src]
fn read_u64(&mut self) -> Result<u64>
[src]
fn read_i64(&mut self) -> Result<i64>
[src]
fn read_f32(&mut self) -> Result<f32>
[src]
fn read_f64(&mut self) -> Result<f64>
[src]
fn read_uint(&mut self, n: usize) -> Result<u64>
[src]
fn read_null_terminated_slice(&mut self) -> Result<Self>
[src]
fn read_uleb128(&mut self) -> Result<u64>
[src]
fn read_uleb128_u16(&mut self) -> Result<u16>
[src]
fn read_sleb128(&mut self) -> Result<i64>
[src]
fn read_initial_length(&mut self) -> Result<(Self::Offset, Format)>
[src]
fn read_address(&mut self, address_size: u8) -> Result<u64>
[src]
fn read_word(&mut self, format: Format) -> Result<Self::Offset>
[src]
fn read_length(&mut self, format: Format) -> Result<Self::Offset>
[src]
fn read_offset(&mut self, format: Format) -> Result<Self::Offset>
[src]
fn read_sized_offset(&mut self, size: u8) -> Result<Self::Offset>
[src]
Auto Trait Implementations
impl<Endian, T> RefUnwindSafe for EndianReader<Endian, T> where
Endian: RefUnwindSafe,
T: RefUnwindSafe,
Endian: RefUnwindSafe,
T: RefUnwindSafe,
impl<Endian, T> Send for EndianReader<Endian, T> where
Endian: Send,
T: Send,
Endian: Send,
T: Send,
impl<Endian, T> Sync for EndianReader<Endian, T> where
Endian: Sync,
T: Sync,
Endian: Sync,
T: Sync,
impl<Endian, T> Unpin for EndianReader<Endian, T> where
Endian: Unpin,
T: Unpin,
Endian: Unpin,
T: Unpin,
impl<Endian, T> UnwindSafe for EndianReader<Endian, T> where
Endian: UnwindSafe,
T: UnwindSafe,
Endian: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> CallHasher for T where
T: Hash,
[src]
T: Hash,
impl<Q, K> Equivalent<K> for Q where
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
[src]
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
pub fn equivalent(&self, key: &K) -> bool
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,