Skip to main content

OwnedMemoryView

Struct OwnedMemoryView 

Source
pub struct OwnedMemoryView { /* private fields */ }
Expand description

Owned memory view that owns its data.

This is a non-reference version of MemoryView that owns the underlying buffer. Useful when the memory view needs to outlive the original scope.

§When to Use OwnedMemoryView vs MemoryView

ScenarioUse
Temporary analysis within a functionMemoryView<&[u8]>
Storing memory data for later useOwnedMemoryView
Returning memory data from a functionOwnedMemoryView
Zero-copy analysisMemoryView<&[u8]>

§Lifetime Management

OwnedMemoryView owns its data via Vec<u8>, so it has no lifetime parameter. This means:

  • The data remains valid as long as the OwnedMemoryView exists
  • No need to worry about the underlying data being dropped
  • Slightly higher memory overhead due to ownership

§Example

use memscope_rs::analysis::unsafe_inference::OwnedMemoryView;

// Create from a vector (takes ownership)
let data = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
let view = OwnedMemoryView::new(data);

// Read values safely
if let Some(value) = view.read_usize(0) {
    println!("First usize: {}", value);
}

// Check bounds
if let Some(byte) = view.read_u8(10) {
    println!("Byte at offset 10: {}", byte);
} else {
    println!("Offset 10 out of bounds");
}

// Access raw slice when needed
let slice = view.as_slice();
println!("Total bytes: {}", slice.len());

§Memory Safety

All read methods perform bounds checking and return Option types. This ensures safe access even with invalid offsets:

use memscope_rs::unsafe_inference::OwnedMemoryView;

let view = OwnedMemoryView::new(vec![0u8; 4]);

// This returns None (out of bounds)
assert!(view.read_usize(0).is_none());

// This returns None (offset + size > len)
assert!(view.read_usize(1).is_none());

Implementations§

Source§

impl OwnedMemoryView

Source

pub fn new(data: Vec<u8>) -> Self

Create a new OwnedMemoryView from a Vec<u8>.

This takes ownership of the vector, so no copying occurs.

§Example
use memscope_rs::unsafe_inference::OwnedMemoryView;
let view = OwnedMemoryView::new(vec![1, 2, 3, 4]);
assert_eq!(view.len(), 4);
Source

pub fn len(&self) -> usize

Returns the length of the underlying data.

§Example
use memscope_rs::unsafe_inference::OwnedMemoryView;
let view = OwnedMemoryView::new(vec![1, 2, 3]);
assert_eq!(view.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if the underlying data is empty.

§Example
use memscope_rs::unsafe_inference::OwnedMemoryView;
let view = OwnedMemoryView::new(vec![]);
assert!(view.is_empty());
Source

pub fn read_usize(&self, offset: usize) -> Option<usize>

Read a usize value from the specified offset.

Reads std::mem::size_of::<usize>() bytes starting at offset and interprets them as a little-endian usize.

Returns None if the read would exceed the buffer bounds.

§Example
use memscope_rs::unsafe_inference::OwnedMemoryView;
let data = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
let view = OwnedMemoryView::new(data);

if let Some(value) = view.read_usize(0) {
    println!("Value: 0x{:x}", value);
}
Source

pub fn read_u8(&self, offset: usize) -> Option<u8>

Read a single byte from the specified offset.

Returns None if the offset is out of bounds.

§Example
use memscope_rs::unsafe_inference::OwnedMemoryView;
let view = OwnedMemoryView::new(vec![0x10, 0x20, 0x30]);

assert_eq!(view.read_u8(0), Some(0x10));
assert_eq!(view.read_u8(2), Some(0x30));
assert_eq!(view.read_u8(3), None); // out of bounds
Source

pub fn as_slice(&self) -> &[u8]

Returns a slice of the underlying data.

This provides direct access to the bytes without copying.

§Example
use memscope_rs::unsafe_inference::OwnedMemoryView;
let view = OwnedMemoryView::new(vec![1, 2, 3, 4, 5]);
let slice = view.as_slice();
assert_eq!(slice, &[1, 2, 3, 4, 5]);
Source

pub fn chunks(&self, chunk_size: usize) -> impl Iterator<Item = &[u8]>

Returns an iterator over chunks of the underlying data.

Each chunk has at most chunk_size elements.

§Example
use memscope_rs::unsafe_inference::OwnedMemoryView;
let view = OwnedMemoryView::new(vec![1, 2, 3, 4, 5, 6]);
let chunks: Vec<_> = view.chunks(2).collect();
assert_eq!(chunks, vec![&[1, 2][..], &[3, 4], &[5, 6]]);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more