Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add windows-version crate #2702

Merged
merged 12 commits into from
Nov 15, 2023
Merged

Add windows-version crate #2702

merged 12 commits into from
Nov 15, 2023

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented Nov 14, 2023

Getting operating system version information simply and reliably is a challenge given the sordid history of versioning APIs on Windows. A simple mechanism that does not require application manifest files is desirable in many instances, particularly for Rust apps that don't necessarily build with Visual Studio and all of its build and packaging conveniences. Here's a simple example:

use windows_version::*;

fn main() {
    println!("Current version: {:?}", OsVersion::current());

    if is_server() {
        println!("Running on a Windows Server release.");
    }

    if OsVersion::current() >= OsVersion::new(10, 0, 0, 12345) {
        println!("Can use a feature available on this version or later.")
    }
}

@kennykerr
Copy link
Collaborator Author

Mainly unsure whether we need something a little less wordy than if Version::new(10, 0, 0, 12345).ge() { ...

Perhaps a helper function like this: if current_ge(10, 0, 0, 12345) {

@ChrisDenton
Copy link
Collaborator

Looks cool!

I suspect the two most common use cases for this would be

  • Diagnostic purposes (e.g. reporting the version in bug reports)
  • Testing if some feature/behaviour can be replied up

I think the former is sufficiently covered by Version::current.

Is there any reason not to implement Ord here? Version::current() > Version::new(10, 0, 0, 12345) would seem nice to have. Especially with some nice const aliases in place of Version::new (not necessarily defined in this crate).

@riverar
Copy link
Collaborator

riverar commented Nov 14, 2023

I think I would prefer more verbose names, favoring the clarity that brings over optimizing keystroke count.

OSVersion::is_greater_than(...)
OSVersion::is_less_than(...)
OSVersion::is_at_least(...)

@kennykerr
Copy link
Collaborator Author

The derived implementation of Ord matches what I need:

https://doc.rust-lang.org/stable/std/cmp/trait.Ord.html#derivable

So that simplifies things.

In terms of verbosity, you can use the Ord and PartialOrd trait-provided methods explicitly to make things explicit if needed.

@damyanp
Copy link
Member

damyanp commented Nov 14, 2023

Windows usually discourages developers from using version checks to determine feature availability. Is there a reason why the Rust APIs should be any treated any differently from other platform APIs?

@riverar
Copy link
Collaborator

riverar commented Nov 14, 2023

Windows usually discourages developers from using version checks to determine feature availability. Is there a reason why the Rust APIs should be any treated any differently from other platform APIs?

The lack of robust (or any) feature detection capabilities in Windows makes that advice impractical.

riverar
riverar previously approved these changes Nov 14, 2023
Copy link
Collaborator

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

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

This looks good to me. I have a few thoughts but nothing that I feel should block merging this.

crates/libs/version/src/lib.rs Outdated Show resolved Hide resolved
crates/libs/version/src/lib.rs Show resolved Hide resolved
@kennykerr kennykerr merged commit a80df1e into master Nov 15, 2023
47 checks passed
@kennykerr kennykerr deleted the version branch November 15, 2023 12:14
@tim-weis
Copy link
Contributor

The lack of robust (or any) feature detection capabilities in Windows makes that advice impractical.

We have

I'd used either of the above for feature detection, and things worked out as advertised.

@riverar Do you have an example where none of the above can be used or report unreliable results? I'm genuinely curious.

@riverar
Copy link
Collaborator

riverar commented Nov 25, 2023

@tim-weis Sure!

We might be able to replace some of these checks with error handling and some QIs but that's more work for no value.

@kennykerr
Copy link
Collaborator Author

LoadLibrary and QueryInterface are both great techniques to use and in many cases that's all that you need, but as @riverar points out there are cases where that is insufficient.

bors added a commit to rust-lang/cargo that referenced this pull request Dec 1, 2023
chore(deps): update rust crate windows-sys to 0.52

[![Mend Renovate logo banner](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [windows-sys](https://togithub.com/microsoft/windows-rs) | workspace.dependencies | minor | `0.48` -> `0.52` |

---

### Release Notes

<details>
<summary>microsoft/windows-rs (windows-sys)</summary>

### [`v0.52.0`](https://togithub.com/microsoft/windows-rs/releases/tag/0.52.0)

[Compare Source](https://togithub.com/microsoft/windows-rs/compare/0.48.0...0.52.0)

This release includes updates to all crates. This includes the first update to the `windows-sys` crate in 8 months. It also includes the first published version of the [riddle](https://crates.io/crates/riddle) tool and the [windows-version](https://crates.io/crates/windows-version) crate.

#### What's Changed

-   Simplify issue templates by [`@&#8203;riverar](https://togithub.com/riverar)` in [microsoft/windows-rs#2621
-   Switch all crates to Rust edition 2021 by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2620
-   Correct workflow trigger ignore paths by [`@&#8203;riverar](https://togithub.com/riverar)` in [microsoft/windows-rs#2622
-   Detect unused `bindgen`/`riddle` filters by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2634
-   Fix `BOOLEAN` parameter binding by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2635
-   Provide individual crate readme files by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2645
-   Tweak Win32 error code conversion to handle `HRESULT` input by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2646
-   Remove support for the defunct `StaticLibrary` attribute by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2647
-   Derive `PartialEq`, `Eq`, `Debug`, `Clone` for interfaces by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2651
-   Internal `bindgen` refactoring by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2654
-   Disable signature transformation for `NTSTATUS` by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2658
-   Unhide `query` method on `ComInterface` trait by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2659
-   Harden `QueryInterface` implementation by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2660
-   Mask non-reproducible linker artifacts in libs by [`@&#8203;riverar](https://togithub.com/riverar)` in [microsoft/windows-rs#2661
-   Slim doc generation by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2671
-   Update SDK and WDK metadata by [`@&#8203;riverar](https://togithub.com/riverar)` in [microsoft/windows-rs#2664
-   Add feature documentation quotes by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2675
-   Add `docs` feature by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2676
-   Simplify metadata reader by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2682
-   Add bindgen config option to disable generating inner attributes by [`@&#8203;dpaoliello](https://togithub.com/dpaoliello)` in [microsoft/windows-rs#2683
-   Simplify metadata filtering by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2684
-   Simplify code generation by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2686
-   Fix link from docs.rs to full API documentation by [`@&#8203;ChrisDenton](https://togithub.com/ChrisDenton)` in [microsoft/windows-rs#2688
-   Optimize tick trimming by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2689
-   Small bindgen refactor and tools refresh by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2695
-   Document `implement` and `interface` macros by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2696
-   Perform checked integral type conversions for APIs by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2699
-   Add `windows-version` crate by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2702
-   Add crate-specific readme files by [`@&#8203;kennykerr](https://togithub.com/kennykerr)` in [microsoft/windows-rs#2703

#### New Contributors

-   [`@&#8203;dpaoliello](https://togithub.com/dpaoliello)` made their first contribution in [microsoft/windows-rs#2683

**Full Changelog**: microsoft/windows-rs@0.48.5...0.52.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 5am on the first day of the month" (UTC), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/rust-lang/cargo).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy41OS44IiwidXBkYXRlZEluVmVyIjoiMzcuNTkuOCIsInRhcmdldEJyYW5jaCI6Im1hc3RlciJ9-->
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.

None yet

5 participants