Skip to content

Conversation

@ldm0
Copy link
Contributor

@ldm0 ldm0 commented May 15, 2025

This commit updates the codebase to use the windows crate instead of the deprecated winapi crate. Key changes include:

  • Replaced winapi imports with windows crate equivalents.
  • Updated ThreadPriorityOsValue from u32 to i32 to align with the new crate's types.
  • Refactored thread priority handling to use the new windows crate enums and functions.
  • Improved type safety and readability by introducing new types for IdealProcessor and ThreadId.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 15, 2025

Walkthrough

The code updates Windows threading support by migrating from the winapi crate to the modern windows crate. It introduces a new ThreadId newtype struct, changes thread priority representations to signed integers, and refactors error handling to use windows crate idioms. Type aliases and enums are updated accordingly.

Changes

File(s) Change Summary
src/windows.rs Migrated from winapi to windows crate for threading APIs, types, and constants.
Replaced ThreadId type alias with a newtype struct and added derived traits and methods.
Changed WinAPIThreadPriority enum to use i32 and windows constants.
Refactored error handling to use windows crate idioms and error types.
Updated function signatures and implementations for new types and error handling.
Updated trait method example to use new ThreadId API.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant ThreadingAPI
    participant WindowsAPI

    Caller->>ThreadingAPI: set_winapi_thread_priority(ThreadId, WinAPIThreadPriority)
    ThreadingAPI->>WindowsAPI: SetThreadPriority(ThreadId.0, WinAPIThreadPriority.0)
    WindowsAPI-->>ThreadingAPI: Result
    ThreadingAPI-->>Caller: Result<(), Error>
Loading
sequenceDiagram
    participant Caller
    participant ThreadingAPI
    participant WindowsAPI

    Caller->>ThreadingAPI: get_thread_priority(ThreadId)
    ThreadingAPI->>WindowsAPI: GetThreadPriority(ThreadId.0)
    WindowsAPI-->>ThreadingAPI: THREAD_PRIORITY
    ThreadingAPI-->>Caller: ThreadPriority or Error
Loading

Poem

A hop from winapi to windows, so bright,
With newtypes and traits, our threads now take flight.
Priorities signed, errors handled with care,
Modern and tidy, with type-safety to spare.
As the code leaps ahead, in the meadow of light,
This rabbit approves—everything feels just right! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 03e5b6a and 70fb23b.

⛔ Files ignored due to path filters (1)
  • Cargo.toml is excluded by !**/*.toml
📒 Files selected for processing (1)
  • src/windows.rs (12 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/windows.rs
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🔭 Outside diff range comments (1)
src/windows.rs (1)

90-106: 🛠️ Refactor suggestion

Gaps in cross-platform→Windows priority mapping (20, 40, 60, 80).

Values 20, 40, 60, and 80 currently fall through the match and raise Error::Priority.
Unless this is deliberate, enlarge the affected ranges so that the entire [0;99] space is accepted.

-1..=19  => WinAPIThreadPriority::Lowest,
-21..=39 => WinAPIThreadPriority::BelowNormal,
-41..=59 => WinAPIThreadPriority::Normal,
-61..=79 => WinAPIThreadPriority::AboveNormal,
-81..=98 => WinAPIThreadPriority::Highest,
+1..=20  => WinAPIThreadPriority::Lowest,
+21..=40 => WinAPIThreadPriority::BelowNormal,
+41..=60 => WinAPIThreadPriority::Normal,
+61..=80 => WinAPIThreadPriority::AboveNormal,
+81..=98 => WinAPIThreadPriority::Highest,
🧹 Nitpick comments (3)
src/windows.rs (3)

25-27: Mark newtype as #[repr(transparent)] for FFI correctness.

IdealProcessor is passed verbatim to the Windows API, so its memory layout must be identical to u32.
Annotating with #[repr(transparent)] makes that guarantee explicit and future-proof.

-#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
-pub struct IdealProcessor(u32);
+#[repr(transparent)]
+#[derive(Clone, Debug, Default, Hash, Eq, PartialEq, Ord, PartialOrd)]
+pub struct IdealProcessor(u32);

29-33: Consider deriving Hash & Ord instead of manual impls.

ThreadId already defines a custom Ord, but the same ordering (by raw handle value) can be obtained automatically with #[derive(Ord, PartialOrd, Hash)] because HANDLE is Copy + Ord.
This removes unsafe casting and reduces maintenance.

If you keep the manual impl, document that ordering by raw address is intentional.


358-375: Duplicated constant list – consider factoring into a helper.

The same exhaustive match on THREAD_PRIORITY values appears twice (TryFrom<ThreadPriority> and here).
Moving the mapping to a single fn map_priority(i32) -> Option<WinAPIThreadPriority> avoids drift and simplifies maintenance.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 64ec5ab and 134bd44.

⛔ Files ignored due to path filters (1)
  • Cargo.toml is excluded by !**/*.toml
📒 Files selected for processing (2)
  • src/lib.rs (1 hunks)
  • src/windows.rs (12 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/windows.rs (2)
src/unix.rs (2)
  • try_from (847-853)
  • thread_native_id (840-842)
src/lib.rs (2)
  • try_from (257-267)
  • ideal_processor (557-560)
🔇 Additional comments (1)
src/lib.rs (1)

279-282: Breaking-change alert: inner type switched from u32i32.

ThreadPriorityOsValue now wraps an i32. This is source-level breaking for anyone pattern-matching or constructing the tuple struct directly.
Please double-check:

  1. Public re-exports / pub use paths that expose the old u32 still compile.
  2. All TryFrom/Into impls in downstream modules (e.g. unix, tests, docs) compile and behave identically for negative values.

If this crate is ≥ 1.0, remember to bump the major version or provide a deprecation shim.

@ldm0 ldm0 force-pushed the ldm_windows_crate branch from 134bd44 to e0a0e99 Compare May 15, 2025 08:53
@iddm
Copy link
Owner

iddm commented May 15, 2025

Wow, thank you so much! I will review it later this week. If I suddenly forget, please ping me! This is certainly useful and a good change to this crate, I'd even call it necessary.

@ldm0 ldm0 force-pushed the ldm_windows_crate branch from e0a0e99 to 035a8f9 Compare May 16, 2025 04:13
if ret == IdealProcessor::max_value() - 1 {
Err(Error::OS(GetLastError() as i32))
let ret = SetThreadIdealProcessor(native.0, ideal_processor);
if ret == u32::MAX {
Copy link
Contributor Author

@ldm0 ldm0 May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ldm0
Copy link
Contributor Author

ldm0 commented May 23, 2025

ping @iddm

@iddm
Copy link
Owner

iddm commented Jun 3, 2025

Hey, thanks again! Would you fix the clippy lints, or would you rather want me to do that?

@ldm0 ldm0 mentioned this pull request Jun 6, 2025
@ldm0 ldm0 force-pushed the ldm_windows_crate branch from 035a8f9 to 03e5b6a Compare June 6, 2025 10:48
@ldm0
Copy link
Contributor Author

ldm0 commented Jun 6, 2025

Hey, thanks again! Would you fix the clippy lints, or would you rather want me to do that?

Hi, #48 was created to resolve these warnings.

…read priority handling

This commit updates the codebase to use the `windows` crate instead of the deprecated `winapi` crate. Key changes include:
- Replaced `winapi` imports with `windows` crate equivalents.
- Refactored thread priority handling to use the new `windows` crate enums and functions.
- Improved type safety and readability by introducing new types for `IdealProcessor` and `ThreadId`.
@ldm0 ldm0 force-pushed the ldm_windows_crate branch from 03e5b6a to 70fb23b Compare June 9, 2025 07:01
@iddm iddm merged commit d0f518b into iddm:master Jun 12, 2025
9 of 10 checks passed
@iddm
Copy link
Owner

iddm commented Jun 12, 2025

Thanks once again!

@iddm
Copy link
Owner

iddm commented Jun 12, 2025

https://crates.io/crates/thread-priority/2.0.0

@ldm0 ldm0 deleted the ldm_windows_crate branch June 12, 2025 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants