Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite39299d

Browse files
committed
Migrate more things to WinError
1 parent818933d commite39299d

File tree

7 files changed

+69
-30
lines changed

7 files changed

+69
-30
lines changed

‎std/src/sys/pal/windows/api.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,39 @@ pub fn get_last_error() -> WinError {
251251
pubstructWinError{
252252
pubcode:u32,
253253
}
254+
implWinError{
255+
constfnnew(code:u32) ->Self{
256+
Self{ code}
257+
}
258+
}
259+
260+
// Error code constants.
261+
// The constant names should be the same as the winapi constants except for the leading `ERROR_`.
262+
// Due to the sheer number of codes, error codes should only be added here on an as-needed basis.
263+
// However, they should never be removed as the assumption is they may be useful again in the future.
264+
#[allow(unused)]
265+
implWinError{
266+
/// Success is not an error.
267+
/// Some Windows APIs do use this to distinguish between a zero return and an error return
268+
/// but we should never return this to users as an error.
269+
pubconstSUCCESS:Self =Self::new(c::ERROR_SUCCESS);
270+
// tidy-alphabetical-start
271+
pubconstACCESS_DENIED:Self =Self::new(c::ERROR_ACCESS_DENIED);
272+
pubconstALREADY_EXISTS:Self =Self::new(c::ERROR_ALREADY_EXISTS);
273+
pubconstCANT_ACCESS_FILE:Self =Self::new(c::ERROR_CANT_ACCESS_FILE);
274+
pubconstDELETE_PENDING:Self =Self::new(c::ERROR_DELETE_PENDING);
275+
pubconstDIRECTORY:Self =Self::new(c::ERROR_DIRECTORY);
276+
pubconstFILE_NOT_FOUND:Self =Self::new(c::ERROR_FILE_NOT_FOUND);
277+
pubconstINSUFFICIENT_BUFFER:Self =Self::new(c::ERROR_INSUFFICIENT_BUFFER);
278+
pubconstINVALID_FUNCTION:Self =Self::new(c::ERROR_INVALID_FUNCTION);
279+
pubconstINVALID_HANDLE:Self =Self::new(c::ERROR_INVALID_HANDLE);
280+
pubconstINVALID_PARAMETER:Self =Self::new(c::ERROR_INVALID_PARAMETER);
281+
pubconstNO_MORE_FILES:Self =Self::new(c::ERROR_NO_MORE_FILES);
282+
pubconstNOT_FOUND:Self =Self::new(c::ERROR_NOT_FOUND);
283+
pubconstNOT_SUPPORTED:Self =Self::new(c::ERROR_NOT_SUPPORTED);
284+
pubconstOPERATION_ABORTED:Self =Self::new(c::ERROR_OPERATION_ABORTED);
285+
pubconstPATH_NOT_FOUND:Self =Self::new(c::ERROR_PATH_NOT_FOUND);
286+
pubconstSHARING_VIOLATION:Self =Self::new(c::ERROR_SHARING_VIOLATION);
287+
pubconstTIMEOUT:Self =Self::new(c::ERROR_TIMEOUT);
288+
// tidy-alphabetical-end
289+
}

‎std/src/sys/pal/windows/fs.rs‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use crate::sys::{c, cvt, Align8};
1818
usecrate::sys_common::{AsInner,FromInner,IntoInner};
1919
usecrate::thread;
2020

21-
usesuper::{api, to_u16s,IoResult};
21+
usesuper::api::{self,WinError};
22+
usesuper::{to_u16s,IoResult};
2223
usecrate::sys::path::maybe_verbatim;
2324

2425
pubstructFile{
@@ -130,10 +131,11 @@ impl Iterator for ReadDir {
130131
letmut wfd = mem::zeroed();
131132
loop{
132133
if c::FindNextFileW(self.handle.0,&mut wfd) ==0{
133-
if api::get_last_error().code == c::ERROR_NO_MORE_FILES{
134-
returnNone;
135-
}else{
136-
returnSome(Err(Error::last_os_error()));
134+
match api::get_last_error(){
135+
WinError::NO_MORE_FILES =>returnNone,
136+
WinError{ code} =>{
137+
returnSome(Err(Error::from_raw_os_error(codeasi32)));
138+
}
137139
}
138140
}
139141
ifletSome(e) =DirEntry::new(&self.root,&wfd){
@@ -244,8 +246,6 @@ impl OpenOptions {
244246
}
245247

246248
fnget_access_mode(&self) -> io::Result<c::DWORD>{
247-
constERROR_INVALID_PARAMETER:i32 =87;
248-
249249
match(self.read,self.write,self.append,self.access_mode){
250250
(..,Some(mode)) =>Ok(mode),
251251
(true,false,false,None) =>Ok(c::GENERIC_READ),
@@ -255,23 +255,23 @@ impl OpenOptions {
255255
(true, _,true,None) =>{
256256
Ok(c::GENERIC_READ |(c::FILE_GENERIC_WRITE& !c::FILE_WRITE_DATA))
257257
}
258-
(false,false,false,None) =>Err(Error::from_raw_os_error(ERROR_INVALID_PARAMETER)),
258+
(false,false,false,None) =>{
259+
Err(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETERasi32))
260+
}
259261
}
260262
}
261263

262264
fnget_creation_mode(&self) -> io::Result<c::DWORD>{
263-
constERROR_INVALID_PARAMETER:i32 =87;
264-
265265
match(self.write,self.append){
266266
(true,false) =>{}
267267
(false,false) =>{
268268
ifself.truncate ||self.create ||self.create_new{
269-
returnErr(Error::from_raw_os_error(ERROR_INVALID_PARAMETER));
269+
returnErr(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETERasi32));
270270
}
271271
}
272272
(_,true) =>{
273273
ifself.truncate && !self.create_new{
274-
returnErr(Error::from_raw_os_error(ERROR_INVALID_PARAMETER));
274+
returnErr(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETERasi32));
275275
}
276276
}
277277
}
@@ -315,7 +315,7 @@ impl File {
315315
// Manual truncation. See #115745.
316316
if opts.truncate
317317
&& creation == c::OPEN_ALWAYS
318-
&&unsafe{ c::GetLastError()}==c::ERROR_ALREADY_EXISTS
318+
&&api::get_last_error() ==WinError::ALREADY_EXISTS
319319
{
320320
unsafe{
321321
// This originally used `FileAllocationInfo` instead of
@@ -845,7 +845,7 @@ fn open_link_no_reparse(parent: &File, name: &[u16], access: u32) -> io::Result<
845845
// We make a special exception for `STATUS_DELETE_PENDING` because
846846
// otherwise this will be mapped to `ERROR_ACCESS_DENIED` which is
847847
// very unhelpful.
848-
Err(io::Error::from_raw_os_error(c::ERROR_DELETE_PENDINGas_))
848+
Err(io::Error::from_raw_os_error(c::ERROR_DELETE_PENDINGasi32))
849849
}elseif status == c::STATUS_INVALID_PARAMETER
850850
&&ATTRIBUTES.load(Ordering::Relaxed) == c::OBJ_DONT_REPARSE
851851
{
@@ -1097,7 +1097,7 @@ pub fn readdir(p: &Path) -> io::Result<ReadDir> {
10971097
//
10981098
// See issue #120040: https://github.com/rust-lang/rust/issues/120040.
10991099
let last_error = api::get_last_error();
1100-
if last_error.code ==c::ERROR_FILE_NOT_FOUND{
1100+
if last_error ==WinError::FILE_NOT_FOUND{
11011101
returnOk(ReadDir{
11021102
handle:FindNextFileHandle(find_handle),
11031103
root:Arc::new(root),

‎std/src/sys/pal/windows/futex.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usesuper::api;
1+
usesuper::api::{self,WinError};
22
usecrate::sys::c;
33
usecrate::sys::dur2timeout;
44
use core::ffi::c_void;
@@ -72,7 +72,7 @@ pub fn wake_by_address_all<T>(address: &T) {
7272

7373
pubfnfutex_wait<W:Waitable>(futex:&W::Atomic,expected:W,timeout:Option<Duration>) ->bool{
7474
// return false only on timeout
75-
wait_on_address(futex, expected, timeout) || api::get_last_error().code !=c::ERROR_TIMEOUT
75+
wait_on_address(futex, expected, timeout) || api::get_last_error() !=WinError::TIMEOUT
7676
}
7777

7878
pubfnfutex_wake<T>(futex:&T) ->bool{

‎std/src/sys/pal/windows/os.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use crate::ptr;
1717
usecrate::slice;
1818
usecrate::sys::{c, cvt};
1919

20-
usesuper::{api, to_u16s};
20+
usesuper::api::{self,WinError};
21+
usesuper::to_u16s;
2122

2223
pubfnerrno() ->i32{
2324
api::get_last_error().codeasi32
@@ -333,7 +334,7 @@ fn home_dir_crt() -> Option<PathBuf> {
333334
buf,
334335
&mut sz,
335336
){
336-
0if api::get_last_error().code !=c::ERROR_INSUFFICIENT_BUFFER =>0,
337+
0if api::get_last_error() !=WinError::INSUFFICIENT_BUFFER =>0,
337338
0 => sz,
338339
_ => sz -1,// sz includes the null terminator
339340
}
@@ -358,7 +359,7 @@ fn home_dir_crt() -> Option<PathBuf> {
358359
super::fill_utf16_buf(
359360
|buf,mut sz|{
360361
match c::GetUserProfileDirectoryW(token, buf,&mut sz){
361-
0if api::get_last_error().code !=c::ERROR_INSUFFICIENT_BUFFER =>0,
362+
0if api::get_last_error() !=WinError::INSUFFICIENT_BUFFER =>0,
362363
0 => sz,
363364
_ => sz -1,// sz includes the null terminator
364365
}

‎std/src/sys/pal/windows/pipe.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::sys::c;
1212
usecrate::sys::fs::{File,OpenOptions};
1313
usecrate::sys::handle::Handle;
1414
usecrate::sys::hashmap_random_keys;
15+
usecrate::sys::pal::windows::api::{self,WinError};
1516
usecrate::sys_common::{FromInner,IntoInner};
1617

1718
////////////////////////////////////////////////////////////////////////////////
@@ -124,20 +125,19 @@ pub fn anon_pipe(ours_readable: bool, their_handle_inheritable: bool) -> io::Res
124125
// testing strategy
125126
// For more info, see https://github.com/rust-lang/rust/pull/37677.
126127
if handle == c::INVALID_HANDLE_VALUE{
127-
let err = io::Error::last_os_error();
128-
let raw_os_err = err.raw_os_error();
128+
let error = api::get_last_error();
129129
if tries <10{
130-
ifraw_os_err ==Some(c::ERROR_ACCESS_DENIEDasi32){
130+
iferror ==WinError::ACCESS_DENIED{
131131
continue;
132132
}elseif reject_remote_clients_flag !=0
133-
&&raw_os_err ==Some(c::ERROR_INVALID_PARAMETERasi32)
133+
&&error ==WinError::INVALID_PARAMETER
134134
{
135135
reject_remote_clients_flag =0;
136136
tries -=1;
137137
continue;
138138
}
139139
}
140-
returnErr(err);
140+
returnErr(io::Error::from_raw_os_error(error.codeasi32));
141141
}
142142
ours =Handle::from_raw_handle(handle);
143143
break;

‎std/src/sys/pal/windows/process.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use crate::sys_common::IntoInner;
3131

3232
use core::ffi::c_void;
3333

34+
usesuper::api::{self,WinError};
35+
3436
////////////////////////////////////////////////////////////////////////////////
3537
// Command
3638
////////////////////////////////////////////////////////////////////////////////
@@ -645,12 +647,12 @@ impl Process {
645647
pubfnkill(&mutself) -> io::Result<()>{
646648
let result =unsafe{ c::TerminateProcess(self.handle.as_raw_handle(),1)};
647649
if result == c::FALSE{
648-
let error =unsafe{ c::GetLastError()};
650+
let error =api::get_last_error();
649651
// TerminateProcess returns ERROR_ACCESS_DENIED if the process has already been
650652
// terminated (by us, or for any other reason). So check if the process was actually
651653
// terminated, and if so, do not return an error.
652-
if error !=c::ERROR_ACCESS_DENIED ||self.try_wait().is_err(){
653-
returnErr(crate::io::Error::from_raw_os_error(errorasi32));
654+
if error !=WinError::ACCESS_DENIED ||self.try_wait().is_err(){
655+
returnErr(crate::io::Error::from_raw_os_error(error.codeasi32));
654656
}
655657
}
656658
Ok(())

‎std/src/sys/pal/windows/stdio.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![unstable(issue ="none", feature ="windows_stdio")]
22

3-
usesuper::api;
3+
usesuper::api::{self,WinError};
44
usecrate::cmp;
55
usecrate::io;
66
usecrate::mem::MaybeUninit;
@@ -370,7 +370,7 @@ fn read_u16s(handle: c::HANDLE, buf: &mut [MaybeUninit<u16>]) -> io::Result<usiz
370370

371371
// ReadConsoleW returns success with ERROR_OPERATION_ABORTED for Ctrl-C or Ctrl-Break.
372372
// Explicitly check for that case here and try again.
373-
if amount ==0 && api::get_last_error().code ==c::ERROR_OPERATION_ABORTED{
373+
if amount ==0 && api::get_last_error() ==WinError::OPERATION_ABORTED{
374374
continue;
375375
}
376376
break;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp