diff --git a/examples/compute/main.rs b/examples/compute/main.rs index 6497c790..02cc442b 100644 --- a/examples/compute/main.rs +++ b/examples/compute/main.rs @@ -19,17 +19,14 @@ fn main() { 24, 25, 26, 27, 28, 29, 30, ]; - let buffer = device.new_buffer_with_data( - unsafe { mem::transmute(data.as_ptr()) }, - (data.len() * mem::size_of::()) as u64, + let buffer = device.new_buffer_from_slice(&data[..], MTLResourceOptions::CPUCacheModeDefaultCache, ); let sum = { let data = [0u32]; - device.new_buffer_with_data( - unsafe { mem::transmute(data.as_ptr()) }, - (data.len() * mem::size_of::()) as u64, + device.new_buffer_from_slice( + &data[..], MTLResourceOptions::CPUCacheModeDefaultCache, ) }; diff --git a/src/device.rs b/src/device.rs index 1cb0a407..4eb3e718 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1965,6 +1965,18 @@ impl DeviceRef { options:options] } } + + pub fn new_buffer_from_slice( + &self, + data:&[T], + options:MTLResourceOptions + )-> Buffer { + self.new_buffer_with_data( + data.as_ptr().cast::(), + (data.len() * std::mem::size_of::()) as NSUInteger, + options + ) + } pub fn new_texture(&self, descriptor: &TextureDescriptorRef) -> Texture { unsafe { msg_send![self, newTextureWithDescriptor: descriptor] }