diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fb57a08..aeee980 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -10,6 +10,7 @@ If you've noticed a bug or have a feature request then please raise a [new issue It's generally best to check the [issues](https://github.com/kenba/opencl3/issues) and [pull requests](https://github.com/kenba/opencl3/pulls) (open and closed) to ensure that someone else has not noticed it before you. I recommend that you wait for confirmation of your bug or approval for your feature request in this way before starting to code. Note: many OpenCL issues are hardware specific, so it is often useful to describe your setup, i.e.: + - `opencl3` features, e.g. ["serde", "CL_VERSION_1_2", "CL_VERSION_2_0", "CL_VERSION_2_1", "CL_VERSION_2_1"] or default - OpenCL target device vendor and version - OpenCL ICD loader vendor and version @@ -23,6 +24,7 @@ Please abide by our [Code of Conduct](CODE_OF_CONDUCT.md) in all issues and pull If the issue is something you think that you can fix, then [fork opencl3](https://docs.github.com/en/get-started/quickstart/fork-a-repo) and create a branch from `develop` with a descriptive name. E.g. a good branch name would be (where issue #42 is the issue you're working on): + ```shell git checkout develop git checkout -b 42-fix-some-bug @@ -31,13 +33,17 @@ git checkout -b 42-fix-some-bug ## Get the test suite running Run the unit tests: + ```shell cargo test -- --test-threads=1 --show-output ``` + and integration tests: + ```shell cargo test -- --test-threads=1 --show-output --ignored ``` + To ensure that you haven't broken anything. Please feel free to add tests, especially where the new test(s) demonstrates a bug that you noticed. @@ -52,10 +58,13 @@ Feel free to ask for help; everyone is a beginner at first. Your patch should follow the same conventions & pass the same code quality checks as the rest of the project. I recommend installing and running `clippy`: + ```shell -cargo clippy +cargo clippy --all-features ``` + and `fmt`: + ```shell cargo fmt ``` @@ -63,17 +72,21 @@ cargo fmt ## Make a Pull Request At this point, you should switch back to your develop branch and make sure it's up to date with opencl3's `develop` branch: + ```shell git remote add upstream git@github.com:kenba/opencl3.git git checkout develop git pull upstream develop ``` + Then update your feature branch from your local copy of master, and push it! + ```shell git checkout 42-fix-some-bug git rebase master git push --set-upstream origin 42-fix-some-bug ``` + Finally, go to GitHub and make a [Pull Request](https://docs.github.com/en/github/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request). Github Actions will then build your PR. @@ -91,6 +104,7 @@ You should *not* introduce a fantastic new feature that you've just thought of! If a maintainer asks you to "rebase" your PR, they're saying that a lot of code has changed, and that you need to update your branch so it's easier to merge. Github have a good guide about [rebasing in Git](https://docs.github.com/en/get-started/using-git/about-git-rebase) here's our suggested workflow: + ```shell git checkout 42-fix-some-bug git pull --rebase upstream develop diff --git a/Cargo.toml b/Cargo.toml index cd87193..a70f6c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,3 +75,7 @@ serde = { version = "1.0", optional = true } [dev-dependencies] serde_json = "1.0" opencl3 = { path = ".", features = ["serde"] } + +[lints.clippy] +enum_glob_use = "deny" +nursery = "deny" \ No newline at end of file diff --git a/src/command_buffer.rs b/src/command_buffer.rs index a545606..24d0768 100644 --- a/src/command_buffer.rs +++ b/src/command_buffer.rs @@ -66,12 +66,12 @@ unsafe impl Send for CommandBuffer {} unsafe impl Sync for CommandBuffer {} impl CommandBuffer { - fn new(buffer: cl_command_buffer_khr) -> CommandBuffer { - CommandBuffer { buffer } + const fn new(buffer: cl_command_buffer_khr) -> Self { + Self { buffer } } /// Get the underlying OpenCL cl_command_buffer_khr. - pub fn get(&self) -> cl_command_buffer_khr { + pub const fn get(&self) -> cl_command_buffer_khr { self.buffer } @@ -79,9 +79,9 @@ impl CommandBuffer { pub fn create( queues: &[cl_command_queue], properties: &[cl_command_buffer_properties_khr], - ) -> Result { + ) -> Result { let buffer = create_command_buffer_khr(queues, properties.as_ptr())?; - Ok(CommandBuffer::new(buffer)) + Ok(Self::new(buffer)) } /// Finalizes command recording ready for enqueuing the command-buffer on a command-queue. @@ -273,6 +273,7 @@ impl CommandBuffer { } /// Records a command to fill a buffer object with a pattern of a given pattern size. + #[allow(clippy::as_ptr_cast_mut)] pub unsafe fn fill_buffer( &self, queue: cl_command_queue, diff --git a/src/command_queue.rs b/src/command_queue.rs index 125685d..f06cd7d 100644 --- a/src/command_queue.rs +++ b/src/command_queue.rs @@ -67,21 +67,21 @@ unsafe impl Send for CommandQueue {} unsafe impl Sync for CommandQueue {} impl CommandQueue { - fn new(queue: cl_command_queue, max_work_item_dimensions: cl_uint) -> CommandQueue { - CommandQueue { + const fn new(queue: cl_command_queue, max_work_item_dimensions: cl_uint) -> Self { + Self { queue, max_work_item_dimensions, } } /// Get the underlying OpenCL cl_command_queue. - pub fn get(&self) -> cl_command_queue { + pub const fn get(&self) -> cl_command_queue { self.queue } /// Get the max_work_item_dimensions for the device that the underlying OpenCL /// device. - pub fn max_work_item_dimensions(&self) -> cl_uint { + pub const fn max_work_item_dimensions(&self) -> cl_uint { self.max_work_item_dimensions } @@ -117,11 +117,11 @@ impl CommandQueue { context: &Context, device_id: cl_device_id, properties: cl_command_queue_properties, - ) -> Result { + ) -> Result { let queue = create_command_queue(context.get(), device_id, properties)?; let device = Device::new(device_id); let max_work_item_dimensions = device.max_work_item_dimensions()?; - Ok(CommandQueue::new(queue, max_work_item_dimensions)) + Ok(Self::new(queue, max_work_item_dimensions)) } /// Create an OpenCL command-queue on the context default device. @@ -150,7 +150,7 @@ impl CommandQueue { pub fn create_default( context: &Context, properties: cl_command_queue_properties, - ) -> Result { + ) -> Result { unsafe { Self::create(context, context.default_device(), properties) } } @@ -175,7 +175,7 @@ impl CommandQueue { device_id: cl_device_id, properties: cl_command_queue_properties, queue_size: cl_uint, - ) -> Result { + ) -> Result { let queue = if (0 < properties) || (0 < queue_size) { let mut props: [cl_queue_properties; 5] = [0; 5]; @@ -197,7 +197,7 @@ impl CommandQueue { let device = Device::new(device_id); let max_work_item_dimensions = device.max_work_item_dimensions()?; - Ok(CommandQueue::new(queue, max_work_item_dimensions)) + Ok(Self::new(queue, max_work_item_dimensions)) } /// Create an OpenCL command-queue on the default device. @@ -215,7 +215,7 @@ impl CommandQueue { context: &Context, properties: cl_command_queue_properties, queue_size: cl_uint, - ) -> Result { + ) -> Result { unsafe { Self::create_with_properties(context, context.default_device(), properties, queue_size) } @@ -226,7 +226,7 @@ impl CommandQueue { context: &Context, device_id: cl_device_id, properties: &[ext::cl_queue_properties_khr], - ) -> Result { + ) -> Result { let queue = ext::create_command_queue_with_properties_khr( context.get(), device_id, @@ -235,7 +235,7 @@ impl CommandQueue { let device = Device::new(device_id); let max_work_item_dimensions = device.max_work_item_dimensions()?; - Ok(CommandQueue::new(queue, max_work_item_dimensions)) + Ok(Self::new(queue, max_work_item_dimensions)) } /// Flush commands to a device. @@ -275,6 +275,7 @@ impl CommandQueue { Ok(Event::new(event)) } + #[allow(clippy::as_ptr_cast_mut)] pub unsafe fn enqueue_read_buffer_rect( &self, buffer: &Buffer, @@ -311,6 +312,7 @@ impl CommandQueue { Ok(Event::new(event)) } + #[allow(clippy::as_ptr_cast_mut)] pub unsafe fn enqueue_write_buffer( &self, buffer: &mut Buffer, @@ -373,6 +375,7 @@ impl CommandQueue { } #[cfg(feature = "CL_VERSION_1_2")] + #[allow(clippy::as_ptr_cast_mut)] pub unsafe fn enqueue_fill_buffer( &self, buffer: &mut Buffer, @@ -804,6 +807,7 @@ impl CommandQueue { Ok(Event::new(event)) } + #[allow(clippy::as_ptr_cast_mut)] pub unsafe fn enqueue_native_kernel( &self, user_func: Option, @@ -973,6 +977,7 @@ impl CommandQueue { } #[cfg(feature = "CL_VERSION_2_0")] + #[allow(clippy::as_ptr_cast_mut)] pub unsafe fn enqueue_svm_unmap( &self, svm: &[T], diff --git a/src/context.rs b/src/context.rs index bbd656e..47fa8f0 100644 --- a/src/context.rs +++ b/src/context.rs @@ -51,6 +51,7 @@ use std::ptr; /// returns a Result containing the device /// or the error code from the OpenCL C API function. #[cfg(feature = "cl_khr_gl_sharing")] +#[allow(clippy::as_ptr_cast_mut)] pub fn get_current_device_for_gl_context_khr( properties: &[cl_context_properties], ) -> Result { @@ -68,6 +69,7 @@ pub fn get_current_device_for_gl_context_khr( /// returns a Result containing the devices /// or the error code from the OpenCL C API function. #[cfg(feature = "cl_khr_gl_sharing")] +#[allow(clippy::as_ptr_cast_mut)] pub fn get_devices_for_gl_context_khr( properties: &[cl_context_properties], ) -> Result> { @@ -108,15 +110,15 @@ unsafe impl Send for Context {} unsafe impl Sync for Context {} impl Context { - fn new(context: cl_context, devices: &[cl_device_id]) -> Context { - Context { + fn new(context: cl_context, devices: &[cl_device_id]) -> Self { + Self { context, devices: devices.to_vec(), } } /// Get the underlying OpenCL cl_context. - pub fn get(&self) -> cl_context { + pub const fn get(&self) -> cl_context { self.context } @@ -135,14 +137,14 @@ impl Context { properties: &[cl_context_properties], pfn_notify: Option, user_data: *mut c_void, - ) -> Result { + ) -> Result { let properties_ptr = if !properties.is_empty() { properties.as_ptr() } else { ptr::null() }; let context = context::create_context(devices, properties_ptr, pfn_notify, user_data)?; - Ok(Context::new(context, devices)) + Ok(Self::new(context, devices)) } /// Create a Context from a [Device]. @@ -151,10 +153,10 @@ impl Context { /// /// returns a Result containing the new OpenCL context /// or the error code from the OpenCL C API function. - pub fn from_device(device: &Device) -> Result { + pub fn from_device(device: &Device) -> Result { let devices: Vec = vec![device.id()]; let properties = Vec::::default(); - Context::from_devices(&devices, &properties, None, ptr::null_mut()) + Self::from_devices(&devices, &properties, None, ptr::null_mut()) } /// Create a Context from a slice of SubDevices. @@ -173,12 +175,12 @@ impl Context { properties: &[cl_context_properties], pfn_notify: Option, user_data: *mut c_void, - ) -> Result { + ) -> Result { let devices = sub_devices .iter() .map(|dev| dev.id()) .collect::>(); - Context::from_devices(&devices, properties, pfn_notify, user_data) + Self::from_devices(&devices, properties, pfn_notify, user_data) } /// Create a Context from a cl_device_type. @@ -196,7 +198,7 @@ impl Context { properties: &[cl_context_properties], pfn_notify: Option, user_data: *mut c_void, - ) -> Result { + ) -> Result { let properties_ptr = if !properties.is_empty() { properties.as_ptr() } else { @@ -210,7 +212,7 @@ impl Context { .iter() .map(|ptr| *ptr as cl_device_id) .collect::>(); - Ok(Context::new(context, &devices)) + Ok(Self::new(context, &devices)) } /// Get the common Shared Virtual Memory (SVM) capabilities of the diff --git a/src/device.rs b/src/device.rs index b20252e..7bab287 100644 --- a/src/device.rs +++ b/src/device.rs @@ -44,7 +44,7 @@ pub struct SubDevice { #[cfg(feature = "CL_VERSION_1_2")] impl From for SubDevice { fn from(id: cl_device_id) -> Self { - SubDevice { id } + Self { id } } } @@ -70,12 +70,12 @@ unsafe impl Sync for SubDevice {} #[cfg(feature = "CL_VERSION_1_2")] impl SubDevice { - pub fn new(id: cl_device_id) -> SubDevice { - SubDevice { id } + pub const fn new(id: cl_device_id) -> Self { + Self { id } } /// Accessor for the underlying device id. - pub fn id(&self) -> cl_device_id { + pub const fn id(&self) -> cl_device_id { self.id } } @@ -90,7 +90,7 @@ pub struct Device { impl From for Device { fn from(value: cl_device_id) -> Self { - Device { + Self { id: value as intptr_t, } } @@ -98,7 +98,7 @@ impl From for Device { impl From for cl_device_id { fn from(value: Device) -> Self { - value.id as cl_device_id + value.id as Self } } @@ -106,12 +106,12 @@ unsafe impl Send for Device {} unsafe impl Sync for Device {} impl Device { - pub fn new(id: cl_device_id) -> Device { - Device { id: id as intptr_t } + pub fn new(id: cl_device_id) -> Self { + Self { id: id as intptr_t } } /// Accessor for the underlying device id. - pub fn id(&self) -> cl_device_id { + pub const fn id(&self) -> cl_device_id { self.id as cl_device_id } @@ -916,22 +916,16 @@ impl Device { /// Determine if the device supports the given half floating point capability. /// Returns true if the device supports it, false otherwise. pub fn supports_half(&self, min_fp_capability: cl_device_fp_config) -> bool { - if let Ok(fp) = self.half_fp_config() { - 0 < fp & min_fp_capability - } else { - false - } + self.half_fp_config() + .map_or(false, |fp| 0 < fp & min_fp_capability) } /// Determine if the device supports the given double floating point capability. /// Returns true if the device supports it, false otherwise. /// /// CL_VERSION_1_2 pub fn supports_double(&self, min_fp_capability: cl_device_fp_config) -> bool { - if let Ok(fp) = self.double_fp_config() { - 0 < fp & min_fp_capability - } else { - false - } + self.double_fp_config() + .map_or(false, |fp| 0 < fp & min_fp_capability) } /// Determine if the device supports SVM and, if so, what kind of SVM. @@ -939,11 +933,7 @@ impl Device { /// /// CL_VERSION_2_0 pub fn svm_mem_capability(&self) -> cl_device_svm_capabilities { - if let Ok(svm) = self.svm_capabilities() { - svm - } else { - 0 - } + self.svm_capabilities().map_or(0, |svm| svm) } #[cfg(feature = "cl_khr_external_semaphore")] diff --git a/src/event.rs b/src/event.rs index 23bf254..5e42763 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2023 Via Technology Ltd. All Rights Reserved. +// Copyright (c) 2020-2024 Via Technology Ltd. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -28,13 +28,13 @@ pub struct Event { impl From for Event { fn from(event: cl_event) -> Self { - Event { event } + Self { event } } } impl From for cl_event { fn from(value: Event) -> Self { - value.event as cl_event + value.event as Self } } @@ -53,12 +53,12 @@ impl Event { /// * `event` - a valid OpenCL cl_event. /// /// returns the new Event - pub fn new(event: cl_event) -> Self { + pub const fn new(event: cl_event) -> Self { Self { event } } /// Get the underlying OpenCL cl_event. - pub fn get(&self) -> cl_event { + pub const fn get(&self) -> cl_event { self.event } diff --git a/src/kernel.rs b/src/kernel.rs index ffa8682..423480e 100644 --- a/src/kernel.rs +++ b/src/kernel.rs @@ -50,7 +50,7 @@ impl Clone for Kernel { /// or the error code from the OpenCL C API function. fn clone(&self) -> Self { let kernel = clone_kernel(self.kernel).expect("Error: clCloneKernel"); - Kernel { kernel } + Self { kernel } } } @@ -70,12 +70,12 @@ impl Kernel { /// returns a Result containing the new Kernel /// or the error code from the OpenCL C API function to get the number /// of kernel arguments. - pub fn new(kernel: cl_kernel) -> Kernel { - Kernel { kernel } + pub const fn new(kernel: cl_kernel) -> Self { + Self { kernel } } /// Get the underlying OpenCL cl_kernel. - pub fn get(&self) -> cl_kernel { + pub const fn get(&self) -> cl_kernel { self.kernel } @@ -87,7 +87,7 @@ impl Kernel { /// returns a Result containing the new Kernel /// or the error code from the OpenCL C API function to get the number /// of kernel arguments. - pub fn create(program: &Program, name: &str) -> Result { + pub fn create(program: &Program, name: &str) -> Result { // Ensure c_name string is null terminated let c_name = CString::new(name).expect("Kernel::create, invalid name"); Ok(Self::new(create_kernel(program.get(), &c_name)?)) diff --git a/src/memory.rs b/src/memory.rs index 9356340..b5b8a3d 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2023 Via Technology Ltd. +// Copyright (c) 2020-2024 Via Technology Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -143,8 +143,8 @@ unsafe impl Send for Buffer {} unsafe impl Sync for Buffer {} impl Buffer { - pub fn new(buffer: cl_mem) -> Buffer { - Buffer { + pub const fn new(buffer: cl_mem) -> Self { + Self { buffer, _type: PhantomData, } @@ -167,10 +167,10 @@ impl Buffer { flags: cl_mem_flags, count: size_t, host_ptr: *mut c_void, - ) -> Result> { + ) -> Result { let buffer = memory::create_buffer(context.get(), flags, count * mem::size_of::(), host_ptr)?; - Ok(Buffer::new(buffer)) + Ok(Self::new(buffer)) } /// Create an OpenCL buffer object for a context. @@ -194,7 +194,7 @@ impl Buffer { flags: cl_mem_flags, count: size_t, host_ptr: *mut c_void, - ) -> Result> { + ) -> Result { let buffer = memory::create_buffer_with_properties( context.get(), properties, @@ -202,7 +202,7 @@ impl Buffer { count * mem::size_of::(), host_ptr, )?; - Ok(Buffer::new(buffer)) + Ok(Self::new(buffer)) } /// Create an OpenCL buffer object for a context from an OpenGL buffer. @@ -219,9 +219,9 @@ impl Buffer { context: &Context, flags: cl_mem_flags, bufobj: gl::cl_GLuint, - ) -> Result> { + ) -> Result { let buffer = gl::create_from_gl_buffer(context.get(), flags, bufobj)?; - Ok(Buffer::new(buffer)) + Ok(Self::new(buffer)) } #[cfg(feature = "cl_intel_create_buffer_with_properties")] @@ -231,7 +231,7 @@ impl Buffer { flags: cl_mem_flags, count: size_t, host_ptr: *mut c_void, - ) -> Result> { + ) -> Result { let buffer = ext::create_buffer_with_properties_intel( context.get(), properties, @@ -239,7 +239,7 @@ impl Buffer { count * mem::size_of::(), host_ptr, )?; - Ok(Buffer::new(buffer)) + Ok(Self::new(buffer)) } /// Create an new OpenCL buffer object from an existing buffer object. @@ -258,7 +258,7 @@ impl Buffer { flags: cl_mem_flags, origin: usize, count: usize, - ) -> Result> { + ) -> Result { let buffer_create_info = cl_buffer_region { origin: origin * std::mem::size_of::(), size: count * std::mem::size_of::(), @@ -269,7 +269,7 @@ impl Buffer { CL_BUFFER_CREATE_TYPE_REGION, &buffer_create_info as *const _ as *const c_void, )?; - Ok(Buffer::new(buffer)) + Ok(Self::new(buffer)) } } @@ -307,8 +307,8 @@ impl Drop for Image { unsafe impl Send for Image {} impl Image { - pub fn new(image: cl_mem) -> Image { - Image { image } + pub const fn new(image: cl_mem) -> Self { + Self { image } } /// Create an OpenCL image object for a context. @@ -333,9 +333,9 @@ impl Image { image_format: *const cl_image_format, image_desc: *const cl_image_desc, host_ptr: *mut c_void, - ) -> Result { + ) -> Result { let image = memory::create_image(context.get(), flags, image_format, image_desc, host_ptr)?; - Ok(Image::new(image)) + Ok(Self::new(image)) } /// Create an OpenCL image object for a context. @@ -363,7 +363,7 @@ impl Image { image_format: *const cl_image_format, image_desc: *const cl_image_desc, host_ptr: *mut c_void, - ) -> Result { + ) -> Result { let image = memory::create_image_with_properties( context.get(), properties, @@ -372,7 +372,7 @@ impl Image { image_desc, host_ptr, )?; - Ok(Image::new(image)) + Ok(Self::new(image)) } /// Create an OpenCL image object, image array object, or image buffer object @@ -395,10 +395,10 @@ impl Image { texture_target: gl::cl_GLenum, miplevel: gl::cl_GLint, texture: gl::cl_GLuint, - ) -> Result { + ) -> Result { let image = gl::create_from_gl_texture(context.get(), flags, texture_target, miplevel, texture)?; - Ok(Image::new(image)) + Ok(Self::new(image)) } /// Create an OpenCL 2D image object from an OpenGL renderbuffer object. @@ -415,9 +415,9 @@ impl Image { context: &Context, flags: cl_mem_flags, renderbuffer: gl::cl_GLuint, - ) -> Result { + ) -> Result { let image = gl::create_from_gl_render_buffer(context.get(), flags, renderbuffer)?; - Ok(Image::new(image)) + Ok(Self::new(image)) } /// Create an OpenCL image object, from the EGLImage source provided as image. @@ -440,10 +440,10 @@ impl Image { image: egl::CLeglImageKHR, flags: cl_mem_flags, properties: &[egl::cl_egl_image_properties_khr], - ) -> Result { + ) -> Result { let image = egl::create_from_egl_image(context.get(), display, image, flags, properties.as_ptr())?; - Ok(Image::new(image)) + Ok(Self::new(image)) } #[cfg(feature = "cl_intel_dx9_media_sharing")] @@ -454,7 +454,7 @@ impl Image { resource: dx9_media_sharing::IDirect3DSurface9_ptr, shared_handle: dx9_media_sharing::HANDLE, plane: cl_uint, - ) -> Result { + ) -> Result { let image = dx9_media_sharing::create_from_dx9_media_surface_intel( context.get(), flags, @@ -462,7 +462,7 @@ impl Image { shared_handle, plane, )?; - Ok(Image::new(image)) + Ok(Self::new(image)) } pub fn format(&self) -> Result> { @@ -559,8 +559,8 @@ impl Drop for Sampler { unsafe impl Send for Sampler {} impl Sampler { - pub fn new(sampler: cl_sampler) -> Sampler { - Sampler { sampler } + pub const fn new(sampler: cl_sampler) -> Self { + Self { sampler } } #[cfg_attr( @@ -580,26 +580,26 @@ impl Sampler { normalize_coords: cl_bool, addressing_mode: cl_addressing_mode, filter_mode: cl_filter_mode, - ) -> Result { + ) -> Result { let sampler = sampler::create_sampler( context.get(), normalize_coords, addressing_mode, filter_mode, )?; - Ok(Sampler::new(sampler)) + Ok(Self::new(sampler)) } #[cfg(feature = "CL_VERSION_2_0")] pub fn create_with_properties( context: &Context, properties: *const cl_sampler_properties, - ) -> Result { + ) -> Result { let sampler = sampler::create_sampler_with_properties(context.get(), properties)?; - Ok(Sampler::new(sampler)) + Ok(Self::new(sampler)) } - pub fn get(&self) -> cl_sampler { + pub const fn get(&self) -> cl_sampler { self.sampler } @@ -653,14 +653,14 @@ pub struct Pipe { #[cfg(feature = "CL_VERSION_2_0")] impl From for Pipe { fn from(pipe: cl_mem) -> Self { - Pipe { pipe } + Self { pipe } } } #[cfg(feature = "CL_VERSION_2_0")] impl From for cl_mem { fn from(value: Pipe) -> Self { - value.pipe as cl_mem + value.pipe as Self } } @@ -684,8 +684,8 @@ impl Drop for Pipe { #[cfg(feature = "CL_VERSION_2_0")] impl Pipe { - pub fn new(pipe: cl_mem) -> Pipe { - Pipe { pipe } + pub const fn new(pipe: cl_mem) -> Self { + Self { pipe } } pub unsafe fn create( @@ -693,9 +693,9 @@ impl Pipe { flags: cl_mem_flags, pipe_packet_size: cl_uint, pipe_max_packets: cl_uint, - ) -> Result { + ) -> Result { let pipe = memory::create_pipe(context.get(), flags, pipe_packet_size, pipe_max_packets)?; - Ok(Pipe::new(pipe)) + Ok(Self::new(pipe)) } pub fn pipe_packet_size(&self) -> Result { diff --git a/src/platform.rs b/src/platform.rs index 2fd098f..9168316 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -42,7 +42,7 @@ pub struct Platform { impl From for Platform { fn from(value: cl_platform_id) -> Self { - Platform { + Self { id: value as intptr_t, } } @@ -50,7 +50,7 @@ impl From for Platform { impl From for cl_platform_id { fn from(value: Platform) -> Self { - value.id as cl_platform_id + value.id as Self } } @@ -58,12 +58,12 @@ unsafe impl Send for Platform {} unsafe impl Sync for Platform {} impl Platform { - pub fn new(id: cl_platform_id) -> Platform { - Platform { id: id as intptr_t } + pub fn new(id: cl_platform_id) -> Self { + Self { id: id as intptr_t } } /// Accessor for the underlying platform id. - pub fn id(&self) -> cl_platform_id { + pub const fn id(&self) -> cl_platform_id { self.id as cl_platform_id } diff --git a/src/program.rs b/src/program.rs index f603b6b..a5e6499 100644 --- a/src/program.rs +++ b/src/program.rs @@ -89,7 +89,7 @@ pub struct Program { impl From for cl_program { fn from(value: Program) -> Self { - value.program as cl_program + value.program as Self } } @@ -103,15 +103,15 @@ unsafe impl Send for Program {} unsafe impl Sync for Program {} impl Program { - fn new(program: cl_program, kernel_names: &str) -> Program { - Program { + fn new(program: cl_program, kernel_names: &str) -> Self { + Self { program, kernel_names: kernel_names.to_owned(), } } /// Get the underlying OpenCL cl_program. - pub fn get(&self) -> cl_program { + pub const fn get(&self) -> cl_program { self.program } @@ -128,8 +128,8 @@ impl Program { /// /// returns a Result containing the new Program /// or the error code from the OpenCL C API function. - pub fn create_from_sources(context: &Context, sources: &[&str]) -> Result { - Ok(Program::new( + pub fn create_from_sources(context: &Context, sources: &[&str]) -> Result { + Ok(Self::new( create_program_with_source(context.get(), sources)?, "", )) @@ -142,9 +142,9 @@ impl Program { /// /// returns a Result containing the new Program /// or the error code from the OpenCL C API function. - pub fn create_from_source(context: &Context, src: &str) -> Result { + pub fn create_from_source(context: &Context, src: &str) -> Result { let sources = [src]; - Ok(Program::new( + Ok(Self::new( create_program_with_source(context.get(), &sources)?, "", )) @@ -166,8 +166,8 @@ impl Program { context: &Context, devices: &[cl_device_id], binaries: &[&[u8]], - ) -> Result { - Ok(Program::new( + ) -> Result { + Ok(Self::new( create_program_with_binary(context.get(), devices, binaries)?, "", )) @@ -191,11 +191,11 @@ impl Program { context: &Context, devices: &[cl_device_id], kernel_names: &str, - ) -> Result { + ) -> Result { // Ensure options string is null terminated let c_names = CString::new(kernel_names) .expect("Program::create_from_builtin_kernels, invalid kernel_names"); - Ok(Program::new( + Ok(Self::new( create_program_with_builtin_kernels(context.get(), devices, &c_names)?, kernel_names, )) @@ -211,13 +211,13 @@ impl Program { /// returns a Result containing the new Program /// or the error code from the OpenCL C API function. #[cfg(feature = "CL_VERSION_2_1")] - pub fn create_from_il(context: &Context, il: &[u8]) -> Result { - Ok(Program::new(create_program_with_il(context.get(), il)?, "")) + pub fn create_from_il(context: &Context, il: &[u8]) -> Result { + Ok(Self::new(create_program_with_il(context.get(), il)?, "")) } #[cfg(feature = "cl_khr_il_program")] - pub fn create_from_il_khr(context: &Context, il: &[u8]) -> Result { - Ok(Program::new( + pub fn create_from_il_khr(context: &Context, il: &[u8]) -> Result { + Ok(Self::new( ext::create_program_with_il_khr(context.get(), il)?, "", )) @@ -253,8 +253,8 @@ impl Program { context: &Context, sources: &[&str], options: &str, - ) -> result::Result { - let mut program = Program::create_from_sources(context, sources).map_err(String::from)?; + ) -> result::Result { + let mut program = Self::create_from_sources(context, sources).map_err(String::from)?; match program.build(context.devices(), options) { Ok(_) => Ok(program), Err(e) => { @@ -282,9 +282,9 @@ impl Program { context: &Context, src: &str, options: &str, - ) -> result::Result { + ) -> result::Result { let sources = [src]; - Program::create_and_build_from_sources(context, &sources, options) + Self::create_and_build_from_sources(context, &sources, options) } /// Create and build an OpenCL Program from binaries with the given options. @@ -299,9 +299,9 @@ impl Program { context: &Context, binaries: &[&[u8]], options: &str, - ) -> Result { + ) -> Result { let mut program = - unsafe { Program::create_from_binary(context, context.devices(), binaries)? }; + unsafe { Self::create_from_binary(context, context.devices(), binaries)? }; program.build(context.devices(), options)?; Ok(program) } @@ -314,15 +314,11 @@ impl Program { /// * `il` - a slice of program intermediate language code. /// * `options` - the build options in a null-terminated string. /// - /// returns a Result containing the new Program + /// returns a Result containing the new `Program` /// or the error code from the OpenCL C API function. #[cfg(feature = "CL_VERSION_2_1")] - pub fn create_and_build_from_il( - context: &Context, - il: &[u8], - options: &str, - ) -> Result { - let mut program = Program::create_from_il(context, il)?; + pub fn create_and_build_from_il(context: &Context, il: &[u8], options: &str) -> Result { + let mut program = Self::create_from_il(context, il)?; program.build(context.devices(), options)?; Ok(program) } diff --git a/src/svm.rs b/src/svm.rs index bdbdb8d..74c779b 100644 --- a/src/svm.rs +++ b/src/svm.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2022 Via Technology Ltd. +// Copyright (c) 2020-2024 Via Technology Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -100,11 +100,12 @@ impl<'a, T> SvmRawVec<'a, T> { fn grow(&mut self, count: usize) -> Result<()> { let elem_size = mem::size_of::(); - let mut new_cap = count; // if pushing or inserting, double the capacity - if (0 < self.cap) && (count - self.cap == 1) { - new_cap = 2 * self.cap; - } + let new_cap = if (0 < self.cap) && (count - self.cap == 1) { + 2 * self.cap + } else { + count + }; let size = elem_size * new_cap; @@ -246,49 +247,48 @@ impl<'a, T> Drop for SvmRawVec<'a, T> { /// # Ok(()) /// # } /// ``` - pub struct SvmVec<'a, T> { buf: SvmRawVec<'a, T>, len: usize, } impl<'a, T> SvmVec<'a, T> { - fn ptr(&self) -> *mut T { + const fn ptr(&self) -> *mut T { self.buf.ptr } /// The capacity of the vector. - pub fn cap(&self) -> usize { + pub const fn cap(&self) -> usize { self.buf.cap } /// The length of the vector. - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.len } /// Whether the vector is empty - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.len == 0 } /// Whether the vector is fine grain buffer - pub fn is_fine_grain_buffer(&self) -> bool { + pub const fn is_fine_grain_buffer(&self) -> bool { self.buf.fine_grain_buffer } /// Whether the vector is fine grain system - pub fn is_fine_grain_system(&self) -> bool { + pub const fn is_fine_grain_system(&self) -> bool { self.buf.fine_grain_system } /// Whether the vector is fine grained - pub fn is_fine_grained(&self) -> bool { + pub const fn is_fine_grained(&self) -> bool { self.buf.fine_grain_buffer || self.buf.fine_grain_system } /// Whether the vector can use atomics - pub fn has_atomics(&self) -> bool { + pub const fn has_atomics(&self) -> bool { self.buf.atomics } @@ -613,7 +613,7 @@ unsafe impl Send for RawValIter {} impl RawValIter { unsafe fn new(slice: &[T]) -> Self { - RawValIter { + Self { start: slice.as_ptr(), end: if mem::size_of::() == 0 { ((slice.as_ptr() as usize) + slice.len()) as *const _