diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 73586d8..c991adf 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/cl3/issues) and [pull requests](https://github.com/kenba/cl3/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.: + - `cl3` features, e.g. ["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 cl3](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,24 +58,29 @@ 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 ``` ## Make a Pull Request At this point, you should switch back to your develop branch and make sure it's up to date with cl3's `develop` branch: + ```shell git remote add upstream git@github.com:kenba/cl3.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. @@ -87,6 +98,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 19e4497..22ec189 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,3 +75,9 @@ thiserror = "1.0" [[example]] name = "clinfo" path = "examples/clinfo.rs" + +[lints.clippy] +enum_glob_use = "deny" +module_name_repetitions = "allow" +nursery = "deny" +unwrap_used = "deny" diff --git a/src/device.rs b/src/device.rs index ed875f2..9b05fbc 100644 --- a/src/device.rs +++ b/src/device.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. @@ -742,7 +742,7 @@ pub const INTEL_DEVICE_VENDOR_ID: cl_uint = 0x8086; pub const AMD_ON_APPLE_DEVICE_VENDOR_ID: cl_uint = 0x1021d00; /// A text representation of an OpenCL vendor id. -pub fn vendor_id_text(vendor_id: cl_uint) -> &'static str { +pub const fn vendor_id_text(vendor_id: cl_uint) -> &'static str { match vendor_id { AMD_DEVICE_VENDOR_ID => "AMD", IBM_DEVICE_VENDOR_ID => "IBM", @@ -763,7 +763,7 @@ pub fn vendor_id_text(vendor_id: cl_uint) -> &'static str { /// A text representation of an OpenCL device type, see: /// [Device Types](https://www.khronos.org/registry/OpenCL/specs/3.0-unified/html/OpenCL_API.html#device-types-table). -pub fn device_type_text(dev_type: cl_device_type) -> &'static str { +pub const fn device_type_text(dev_type: cl_device_type) -> &'static str { match dev_type { CL_DEVICE_TYPE_DEFAULT => "CL_DEVICE_TYPE_DEFAULT", CL_DEVICE_TYPE_CPU => "CL_DEVICE_TYPE_CPU", diff --git a/src/error_codes.rs b/src/error_codes.rs index 9d271d3..c4a70c5 100644 --- a/src/error_codes.rs +++ b/src/error_codes.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. @@ -55,7 +55,7 @@ pub use opencl_sys::cl_egl::{CL_EGL_RESOURCE_NOT_ACQUIRED_KHR, CL_INVALID_EGL_OB use std::fmt; use thiserror::Error; -pub fn error_text(error_code: cl_int) -> &'static str { +pub const fn error_text(error_code: cl_int) -> &'static str { match error_code { CL_SUCCESS => "CL_SUCCESS", CL_DEVICE_NOT_FOUND => "CL_DEVICE_NOT_FOUND", @@ -171,7 +171,7 @@ pub struct ClError(pub cl_int); /// Implement the From trait impl From for ClError { fn from(error: cl_int) -> Self { - ClError(error) + Self(error) } } @@ -185,7 +185,7 @@ impl From for &str { /// Implement the From trait for String impl From for String { fn from(error: ClError) -> Self { - String::from(error_text(error.0)) + Self::from(error_text(error.0)) } } diff --git a/src/event.rs b/src/event.rs index 4083c1f..41cce90 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,4 +1,4 @@ -// Copyright (c) 2020-2021 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. @@ -254,7 +254,7 @@ pub fn get_event_profiling_info( } } -pub fn status_text(status: cl_int) -> &'static str { +pub const fn status_text(status: cl_int) -> &'static str { match status { CL_COMPLETE => "CL_COMPLETE", CL_RUNNING => "CL_RUNNING", @@ -271,7 +271,7 @@ pub struct CommandExecutionStatus(pub cl_int); /// Implement the From trait impl From for CommandExecutionStatus { fn from(status: cl_int) -> Self { - CommandExecutionStatus(status) + Self(status) } } @@ -282,7 +282,7 @@ impl fmt::Display for CommandExecutionStatus { } } -pub fn command_type_text(command_type: cl_command_type) -> &'static str { +pub const fn command_type_text(command_type: cl_command_type) -> &'static str { match command_type { CL_COMMAND_NDRANGE_KERNEL => "CL_COMMAND_NDRANGE_KERNEL", CL_COMMAND_TASK => "CL_COMMAND_TASK", @@ -341,7 +341,7 @@ pub struct EventCommandType(pub cl_command_type); /// Implement the From trait for EventCommandType impl From for EventCommandType { fn from(command_type: cl_command_type) -> Self { - EventCommandType(command_type) + Self(command_type) } } diff --git a/src/info_type.rs b/src/info_type.rs index 737a953..83526a9 100644 --- a/src/info_type.rs +++ b/src/info_type.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. @@ -156,26 +156,26 @@ impl From for String { let mut a = Vec::::from(info_type); // remove all trailing nulls, if any - while let Some(0) = a.last() { + while a.last() == Some(&0) { a.pop(); } // convert invalid characters to std::char::REPLACEMENT_CHARACTER - String::from_utf8_lossy(&a).into_owned() + Self::from_utf8_lossy(&a).into_owned() } } impl fmt::Display for InfoType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - InfoType::VecUchar(a) => { + Self::VecUchar(a) => { let b = String::from_utf8_lossy(a).into_owned(); write!(f, "{}", b) } // Formats a LUID the same way as `clinfo`. // See: https://github.com/Oblomov/clinfo/blob/master/src/clinfo.c - InfoType::Luid(a) => { + Self::Luid(a) => { write!( f, "{:x}{:x}-{:x}{:x}{:x}{:x}{:x}{:x}", @@ -184,7 +184,7 @@ impl fmt::Display for InfoType { } // Formats a UUID according to RFC4122. - InfoType::Uuid(a) => { + Self::Uuid(a) => { write!( f, "{:x}{:x}{:x}{:x}-{:x}{:x}-{:x}{:x}-{:x}{:x}-{:x}{:x}{:x}{:x}{:x}{:x}", @@ -207,7 +207,7 @@ impl fmt::Display for InfoType { ) } - InfoType::VecNameVersion(a) => { + Self::VecNameVersion(a) => { let mut s = String::default(); for b in a.iter() { s.push('\n'); @@ -220,7 +220,7 @@ impl fmt::Display for InfoType { write!(f, "{}", s) } - InfoType::VecImageFormat(a) => { + Self::VecImageFormat(a) => { let mut s = String::default(); for b in a.iter() { @@ -236,7 +236,7 @@ impl fmt::Display for InfoType { // Note: underlying type may not be a vector of Strings. // If so use Debug trait instead - InfoType::VecVecUchar(a) => { + Self::VecVecUchar(a) => { let mut s = String::default(); for b in a.iter() { s.push('\n');