pub struct Region<'cx, T: Binary> { /* private fields */ }
Expand description
Represents a typed region of an ArrayBuffer
.
A Region
can be created via the
Handle<JsArrayBuffer>::region()
or
JsTypedArray::region()
methods.
A region is not checked for validity until it is converted to
a typed array via to_typed_array()
or
JsTypedArray::from_region()
.
Example
// Allocate a 16-byte ArrayBuffer and a uint32 array of length 2 (i.e., 8 bytes)
// starting at byte offset 4 of the buffer:
//
// 0 4 8 12 16
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// buf: | | | | | | | | | | | | | | | | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// ^ ^
// | |
// +-------+-------+
// arr: | | |
// +-------+-------+
// 0 1 2
let buf = cx.array_buffer(16)?;
let arr = JsUint32Array::from_region(&mut cx, &buf.region(4, 2))?;
Implementations
sourceimpl<'cx, T: Binary> Region<'cx, T>
impl<'cx, T: Binary> Region<'cx, T>
sourcepub fn buffer(&self) -> Handle<'cx, JsArrayBuffer>
pub fn buffer(&self) -> Handle<'cx, JsArrayBuffer>
Returns the handle to the region’s buffer.
sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the size of the region in bytes, which is equal to
(self.len() * size_of::<T>())
.
sourcepub fn to_typed_array<'c, C>(&self, cx: &mut C) -> JsResult<'c, JsTypedArray<T>>where
C: Context<'c>,
pub fn to_typed_array<'c, C>(&self, cx: &mut C) -> JsResult<'c, JsTypedArray<T>>where
C: Context<'c>,
Constructs a typed array for this buffer region.
The resulting typed array has self.len()
elements and a size of
self.size()
bytes.
Throws an exception if the region is invalid, for example if the starting offset is not properly aligned, or the length goes beyond the end of the buffer.
Trait Implementations
impl<'cx, T: Copy + Binary> Copy for Region<'cx, T>
Auto Trait Implementations
impl<'cx, T> RefUnwindSafe for Region<'cx, T>where
T: RefUnwindSafe,
impl<'cx, T> !Send for Region<'cx, T>
impl<'cx, T> !Sync for Region<'cx, T>
impl<'cx, T> Unpin for Region<'cx, T>where
T: Unpin,
impl<'cx, T> UnwindSafe for Region<'cx, T>where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more