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

Migrate from winapi to windows-sys. #62

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: |
rustup toolchain install 1.34.0
rustup default 1.34.0
rustup toolchain install 1.48.0
rustup default 1.48.0
rustup target add \
x86_64-pc-windows-gnu \
x86_64-apple-darwin \
Expand Down
2 changes: 1 addition & 1 deletion directories/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Other platforms are also supported; they use the Linux conventions.

## Minimum Rust version policy

The minimal required version of Rust is `1.34.0`.
The minimal required version of Rust is `1.48.0`.

We may bump the Rust version in major and minor releases (`x`/`y` in `x.y.z`).
Changing the Rust version will be written in the CHANGELOG.
Expand Down
10 changes: 8 additions & 2 deletions dirs-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ libc = "0.2"
[target.'cfg(target_os = "redox")'.dependencies]
redox_users = { version = "0.4.0", default-features = false }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["knownfolders", "objbase", "shlobj", "winbase", "winerror"] }
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.45.0"
features = [
"Win32_Foundation",
"Win32_Globalization",
"Win32_System_Com",
"Win32_UI_Shell",
]
2 changes: 1 addition & 1 deletion dirs-sys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Other platforms are also supported; they use the Linux conventions.

## Minimum Rust version policy

The minimal required version of Rust is `1.34.0`^.
The minimal required version of Rust is `1.48.0`^.

We may bump the Rust version in major and minor releases (`x`/`y` in `x.y.z`).
Changing the Rust version will be written in the CHANGELOG.
Expand Down
56 changes: 29 additions & 27 deletions dirs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,65 +128,67 @@ mod target_windows {
use std::ptr;
use std::slice;

use winapi::shared::winerror;
use winapi::um::{combaseapi, knownfolders, shlobj, shtypes, winbase, winnt};

pub fn known_folder(folder_id: shtypes::REFKNOWNFOLDERID) -> Option<PathBuf> {
unsafe {
let mut path_ptr: winnt::PWSTR = ptr::null_mut();
let result = shlobj::SHGetKnownFolderPath(folder_id, 0, ptr::null_mut(), &mut path_ptr);
if result == winerror::S_OK {
let len = winbase::lstrlenW(path_ptr) as usize;
let path = slice::from_raw_parts(path_ptr, len);
let ostr: OsString = OsStringExt::from_wide(path);
combaseapi::CoTaskMemFree(path_ptr as *mut winapi::ctypes::c_void);
Some(PathBuf::from(ostr))
} else {
None
}
use windows_sys::core::GUID;
use windows_sys::core::PWSTR;
use windows_sys::Win32::Foundation::S_OK;
use windows_sys::Win32::Globalization::lstrlenW;
use windows_sys::Win32::System::Com::CoTaskMemFree;
use windows_sys::Win32::UI::Shell;

pub unsafe fn known_folder(folder_id: *const GUID) -> Option<PathBuf> {
let mut path_ptr: PWSTR = ptr::null_mut();
let result = Shell::SHGetKnownFolderPath(folder_id, 0, 0, &mut path_ptr);
if result == S_OK {
let len = lstrlenW(path_ptr) as usize;
let path = slice::from_raw_parts(path_ptr, len);
let ostr: OsString = OsStringExt::from_wide(path);
CoTaskMemFree(path_ptr as *mut std::os::raw::c_void);
Some(PathBuf::from(ostr))
} else {
None
}
}

pub fn known_folder_profile() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Profile)
unsafe { known_folder(&Shell::FOLDERID_Profile) }
}

pub fn known_folder_roaming_app_data() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_RoamingAppData)
unsafe { known_folder(&Shell::FOLDERID_RoamingAppData) }
}

pub fn known_folder_local_app_data() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_LocalAppData)
unsafe { known_folder(&Shell::FOLDERID_LocalAppData) }
}

pub fn known_folder_music() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Music)
unsafe { known_folder(&Shell::FOLDERID_Music) }
}

pub fn known_folder_desktop() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Desktop)
unsafe { known_folder(&Shell::FOLDERID_Desktop) }
}

pub fn known_folder_documents() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Documents)
unsafe { known_folder(&Shell::FOLDERID_Documents) }
}

pub fn known_folder_downloads() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Downloads)
unsafe { known_folder(&Shell::FOLDERID_Downloads) }
}

pub fn known_folder_pictures() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Pictures)
unsafe { known_folder(&Shell::FOLDERID_Pictures) }
}

pub fn known_folder_public() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Public)
unsafe { known_folder(&Shell::FOLDERID_Public) }
}
pub fn known_folder_templates() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Templates)
unsafe { known_folder(&Shell::FOLDERID_Templates) }
}
pub fn known_folder_videos() -> Option<PathBuf> {
known_folder(&knownfolders::FOLDERID_Videos)
unsafe { known_folder(&Shell::FOLDERID_Videos) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion dirs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Other platforms are also supported; they use the Linux conventions.

## Minimum Rust version policy

The minimal required version of Rust is `1.34.0`.
The minimal required version of Rust is `1.48.0`.

We may bump the Rust version in major and minor releases (`x`/`y` in `x.y.z`).
Changing the Rust version will be written in the CHANGELOG.
Expand Down