pub struct Buffer { /* private fields */ }Expand description
A buffer that knows where its bytes live.
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn new_host(shape: Shape, data: Vec<u8>) -> Self
pub fn new_host(shape: Shape, data: Vec<u8>) -> Self
Create a buffer holding data, tagged as residing on device.
On Cpu this is just storage; on Metal callers will use
to_device to move it into device memory once the runtime
integrates with the arena.
pub fn shape(&self) -> &Shape
pub fn device(&self) -> Device
pub fn dtype(&self) -> DType
pub fn num_elements(&self) -> usize
pub fn byte_size(&self) -> usize
Sourcepub fn as_f32(&self) -> &[f32]
pub fn as_f32(&self) -> &[f32]
Read as &[f32]. Panics if dtype isn’t F32 — explicit type
check (plan #59 spirit: don’t let mismatches go silent).
pub fn as_f32_mut(&mut self) -> &mut [f32]
Sourcepub fn to_device(self, device: Device) -> Self
pub fn to_device(self, device: Device) -> Self
“Move to device” — explicit transfer call. CPU is a no-op (the bytes are already where they need to be). Metal routes through the backend (TODO: wire to rlx-metal::arena once a caller needs it).
Sourcepub fn to_host(self) -> Self
pub fn to_host(self) -> Self
“Read back to host” — explicit transfer. CPU no-op; Metal blocks on completion + memcpy back from MTLBuffer (TODO).
Sourcepub fn host_bytes(&self) -> &[u8] ⓘ
pub fn host_bytes(&self) -> &[u8] ⓘ
Raw bytes — host-side access. Panics if the buffer is on a non-host device (would silently read uninitialized host memory otherwise).