spatialrust_gpu/
buffer.rs1use spatialrust_core::{Device, DeviceKind, SpatialError, SpatialResult};
2
3#[derive(Clone, Debug, PartialEq, Eq)]
5pub struct DeviceBuffer<T> {
6 len: usize,
7 device_kind: DeviceKind,
8 _marker: core::marker::PhantomData<T>,
9}
10
11impl<T> DeviceBuffer<T> {
12 pub fn new(len: usize, device: &dyn Device) -> SpatialResult<Self> {
16 if len == 0 {
17 return Err(SpatialError::InvalidArgument(
18 "device buffer length must be greater than zero".to_owned(),
19 ));
20 }
21
22 Ok(Self { len, device_kind: device.kind(), _marker: core::marker::PhantomData })
23 }
24
25 #[must_use]
27 pub const fn len(&self) -> usize {
28 self.len
29 }
30
31 #[must_use]
33 pub const fn is_empty(&self) -> bool {
34 self.len == 0
35 }
36
37 #[must_use]
39 pub const fn device_kind(&self) -> DeviceKind {
40 self.device_kind
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::DeviceBuffer;
47 use spatialrust_core::CpuDevice;
48
49 #[test]
50 fn rejects_zero_length_buffer() {
51 let device = CpuDevice;
52 let err = DeviceBuffer::<f32>::new(0, &device).unwrap_err();
53 assert!(matches!(err, spatialrust_core::SpatialError::InvalidArgument(_)));
54 }
55}