Skip to content

Commit

Permalink
Fix clippy nursery lints.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenba committed Mar 31, 2024
1 parent f8cb228 commit cbb312f
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 152 deletions.
16 changes: 15 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.

Expand All @@ -52,28 +58,35 @@ 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
```

## 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 [email protected]: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.
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 6 additions & 5 deletions src/command_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,22 @@ 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
}

/// Create a command-buffer that can record commands to the specified queues.
pub fn create(
queues: &[cl_command_queue],
properties: &[cl_command_buffer_properties_khr],
) -> Result<CommandBuffer> {
) -> Result<Self> {
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.
Expand Down Expand Up @@ -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<T>(
&self,
queue: cl_command_queue,
Expand Down
29 changes: 17 additions & 12 deletions src/command_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -117,11 +117,11 @@ impl CommandQueue {
context: &Context,
device_id: cl_device_id,
properties: cl_command_queue_properties,
) -> Result<CommandQueue> {
) -> Result<Self> {
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.
Expand Down Expand Up @@ -150,7 +150,7 @@ impl CommandQueue {
pub fn create_default(
context: &Context,
properties: cl_command_queue_properties,
) -> Result<CommandQueue> {
) -> Result<Self> {
unsafe { Self::create(context, context.default_device(), properties) }
}

Expand All @@ -175,7 +175,7 @@ impl CommandQueue {
device_id: cl_device_id,
properties: cl_command_queue_properties,
queue_size: cl_uint,
) -> Result<CommandQueue> {
) -> Result<Self> {
let queue = if (0 < properties) || (0 < queue_size) {
let mut props: [cl_queue_properties; 5] = [0; 5];

Expand All @@ -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.
Expand All @@ -215,7 +215,7 @@ impl CommandQueue {
context: &Context,
properties: cl_command_queue_properties,
queue_size: cl_uint,
) -> Result<CommandQueue> {
) -> Result<Self> {
unsafe {
Self::create_with_properties(context, context.default_device(), properties, queue_size)
}
Expand All @@ -226,7 +226,7 @@ impl CommandQueue {
context: &Context,
device_id: cl_device_id,
properties: &[ext::cl_queue_properties_khr],
) -> Result<CommandQueue> {
) -> Result<Self> {
let queue = ext::create_command_queue_with_properties_khr(
context.get(),
device_id,
Expand All @@ -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.
Expand Down Expand Up @@ -275,6 +275,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[allow(clippy::as_ptr_cast_mut)]
pub unsafe fn enqueue_read_buffer_rect<T>(
&self,
buffer: &Buffer<T>,
Expand Down Expand Up @@ -311,6 +312,7 @@ impl CommandQueue {
Ok(Event::new(event))
}

#[allow(clippy::as_ptr_cast_mut)]
pub unsafe fn enqueue_write_buffer<T>(
&self,
buffer: &mut Buffer<T>,
Expand Down Expand Up @@ -373,6 +375,7 @@ impl CommandQueue {
}

#[cfg(feature = "CL_VERSION_1_2")]
#[allow(clippy::as_ptr_cast_mut)]
pub unsafe fn enqueue_fill_buffer<T>(
&self,
buffer: &mut Buffer<T>,
Expand Down Expand Up @@ -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<unsafe extern "C" fn(*mut c_void)>,
Expand Down Expand Up @@ -973,6 +977,7 @@ impl CommandQueue {
}

#[cfg(feature = "CL_VERSION_2_0")]
#[allow(clippy::as_ptr_cast_mut)]
pub unsafe fn enqueue_svm_unmap<T>(
&self,
svm: &[T],
Expand Down
24 changes: 13 additions & 11 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<cl_device_id> {
Expand All @@ -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<Vec<cl_device_id>> {
Expand Down Expand Up @@ -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
}

Expand All @@ -135,14 +137,14 @@ impl Context {
properties: &[cl_context_properties],
pfn_notify: Option<unsafe extern "C" fn(*const c_char, *const c_void, size_t, *mut c_void)>,
user_data: *mut c_void,
) -> Result<Context> {
) -> Result<Self> {
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].
Expand All @@ -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<Context> {
pub fn from_device(device: &Device) -> Result<Self> {
let devices: Vec<cl_device_id> = vec![device.id()];
let properties = Vec::<cl_context_properties>::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.
Expand All @@ -173,12 +175,12 @@ impl Context {
properties: &[cl_context_properties],
pfn_notify: Option<unsafe extern "C" fn(*const c_char, *const c_void, size_t, *mut c_void)>,
user_data: *mut c_void,
) -> Result<Context> {
) -> Result<Self> {
let devices = sub_devices
.iter()
.map(|dev| dev.id())
.collect::<Vec<cl_device_id>>();
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.
Expand All @@ -196,7 +198,7 @@ impl Context {
properties: &[cl_context_properties],
pfn_notify: Option<unsafe extern "C" fn(*const c_char, *const c_void, size_t, *mut c_void)>,
user_data: *mut c_void,
) -> Result<Context> {
) -> Result<Self> {
let properties_ptr = if !properties.is_empty() {
properties.as_ptr()
} else {
Expand All @@ -210,7 +212,7 @@ impl Context {
.iter()
.map(|ptr| *ptr as cl_device_id)
.collect::<Vec<cl_device_id>>();
Ok(Context::new(context, &devices))
Ok(Self::new(context, &devices))
}

/// Get the common Shared Virtual Memory (SVM) capabilities of the
Expand Down
Loading

0 comments on commit cbb312f

Please sign in to comment.