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
| Scenario | Use |
|---|---|
| Temporary analysis within a function | MemoryView<&[u8]> |
| Storing memory data for later use | OwnedMemoryView |
| Returning memory data from a function | OwnedMemoryView |
| Zero-copy analysis | MemoryView<&[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
OwnedMemoryViewexists - 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
impl OwnedMemoryView
Sourcepub fn new(data: Vec<u8>) -> Self
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);Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn read_usize(&self, offset: usize) -> Option<usize>
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);
}Sourcepub fn read_u8(&self, offset: usize) -> Option<u8>
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 boundsSourcepub fn as_slice(&self) -> &[u8] ⓘ
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]);Sourcepub fn chunks(&self, chunk_size: usize) -> impl Iterator<Item = &[u8]>
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§
impl Freeze for OwnedMemoryView
impl RefUnwindSafe for OwnedMemoryView
impl Send for OwnedMemoryView
impl Sync for OwnedMemoryView
impl Unpin for OwnedMemoryView
impl UnsafeUnpin for OwnedMemoryView
impl UnwindSafe for OwnedMemoryView
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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