Skip to content

Commit ded5704

Browse files
authored
Merge pull request #89 from taiki-e/try
Replace r#try! macro with ? operator
2 parents ccdc47b + 809a89d commit ded5704

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

src/remove_dir_all.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ mod win {
103103
opts.access_mode(FILE_READ_ATTRIBUTES);
104104
opts.custom_flags(FILE_FLAG_BACKUP_SEMANTICS |
105105
FILE_FLAG_OPEN_REPARSE_POINT);
106-
let file = r#try!(File::open(path, &opts));
107-
(r#try!(get_path(&file)), r#try!(file.file_attr()))
106+
let file = File::open(path, &opts)?;
107+
(get_path(&file)?, file.file_attr()?)
108108
};
109109

110110
let mut ctx = RmdirContext {
@@ -131,7 +131,7 @@ mod win {
131131
fn readdir(p: &Path) -> io::Result<ReadDir> {
132132
let root = p.to_path_buf();
133133
let star = p.join("*");
134-
let path = r#try!(to_u16s(&star));
134+
let path = to_u16s(&star)?;
135135

136136
unsafe {
137137
let mut wfd = mem::zeroed();
@@ -157,14 +157,14 @@ mod win {
157157
fn remove_dir_all_recursive(path: &Path, ctx: &mut RmdirContext)
158158
-> io::Result<()> {
159159
let dir_readonly = ctx.readonly;
160-
for child in r#try!(readdir(path)) {
161-
let child = r#try!(child);
162-
let child_type = r#try!(child.file_type());
163-
ctx.readonly = r#try!(child.metadata()).perm().readonly();
160+
for child in readdir(path)? {
161+
let child = child?;
162+
let child_type = child.file_type()?;
163+
ctx.readonly = child.metadata()?.perm().readonly();
164164
if child_type.is_dir() {
165-
r#try!(remove_dir_all_recursive(&child.path(), ctx));
165+
remove_dir_all_recursive(&child.path(), ctx)?;
166166
} else {
167-
r#try!(remove_item(&child.path().as_ref(), ctx));
167+
remove_item(&child.path().as_ref(), ctx)?;
168168
}
169169
}
170170
ctx.readonly = dir_readonly;
@@ -178,20 +178,20 @@ mod win {
178178
opts.custom_flags(FILE_FLAG_BACKUP_SEMANTICS | // delete directory
179179
FILE_FLAG_OPEN_REPARSE_POINT | // delete symlink
180180
FILE_FLAG_DELETE_ON_CLOSE);
181-
let file = r#try!(File::open(path, &opts));
181+
let file = File::open(path, &opts)?;
182182
move_item(&file, ctx)
183183
} else {
184184
// remove read-only permision
185-
r#try!(set_perm(&path, FilePermissions::new()));
185+
set_perm(&path, FilePermissions::new())?;
186186
// move and delete file, similar to !readonly.
187187
// only the access mode is different.
188188
let mut opts = OpenOptions::new();
189189
opts.access_mode(DELETE | FILE_WRITE_ATTRIBUTES);
190190
opts.custom_flags(FILE_FLAG_BACKUP_SEMANTICS |
191191
FILE_FLAG_OPEN_REPARSE_POINT |
192192
FILE_FLAG_DELETE_ON_CLOSE);
193-
let file = r#try!(File::open(path, &opts));
194-
r#try!(move_item(&file, ctx));
193+
let file = File::open(path, &opts)?;
194+
move_item(&file, ctx)?;
195195
// restore read-only flag just in case there are other hard links
196196
let mut perm = FilePermissions::new();
197197
perm.set_readonly(true);
@@ -445,13 +445,13 @@ mod win {
445445

446446
impl File {
447447
fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
448-
let path = r#try!(to_u16s(path));
448+
let path = to_u16s(path)?;
449449
let handle = unsafe {
450450
CreateFileW(path.as_ptr(),
451-
r#try!(opts.get_access_mode()),
451+
opts.get_access_mode()?,
452452
opts.share_mode,
453453
opts.security_attributes as *mut _,
454-
r#try!(opts.get_creation_mode()),
454+
opts.get_creation_mode()?,
455455
opts.get_flags_and_attributes(),
456456
ptr::null_mut())
457457
};
@@ -465,8 +465,8 @@ mod win {
465465
fn file_attr(&self) -> io::Result<FileAttr> {
466466
unsafe {
467467
let mut info: BY_HANDLE_FILE_INFORMATION = mem::zeroed();
468-
r#try!(cvt(GetFileInformationByHandle(self.handle.raw(),
469-
&mut info)));
468+
cvt(GetFileInformationByHandle(self.handle.raw(),
469+
&mut info))?;
470470
let mut attr = FileAttr {
471471
attributes: info.dwFileAttributes,
472472
creation_time: info.ftCreationTime,
@@ -498,12 +498,12 @@ mod win {
498498
FileAttributes: attr,
499499
};
500500
let size = mem::size_of_val(&info);
501-
r#try!(cvt(unsafe {
501+
cvt(unsafe {
502502
SetFileInformationByHandle(self.handle.raw(),
503503
FileBasicInfo,
504504
&mut info as *mut _ as *mut _,
505505
size as DWORD)
506-
}));
506+
})?;
507507
Ok(())
508508
}
509509

@@ -531,15 +531,15 @@ mod win {
531531
(*info).ReplaceIfExists = if replace { -1 } else { FALSE };
532532
(*info).RootDirectory = ptr::null_mut();
533533
(*info).FileNameLength = (size - STRUCT_SIZE) as DWORD;
534-
r#try!(cvt(SetFileInformationByHandle(self.handle().raw(),
534+
cvt(SetFileInformationByHandle(self.handle().raw(),
535535
FileRenameInfo,
536536
data.as_mut_ptr() as *mut _ as *mut _,
537-
size as DWORD)));
537+
size as DWORD))?;
538538
Ok(())
539539
}
540540
}
541541
fn set_perm(&self, perm: FilePermissions) -> io::Result<()> {
542-
let attr = r#try!(self.file_attr()).attributes;
542+
let attr = self.file_attr()?.attributes;
543543
if perm.readonly == (attr & FILE_ATTRIBUTE_READONLY != 0) {
544544
Ok(())
545545
} else if perm.readonly {
@@ -556,7 +556,7 @@ mod win {
556556
-> io::Result<(DWORD, &'a REPARSE_DATA_BUFFER)> {
557557
unsafe {
558558
let mut bytes = 0;
559-
r#try!(cvt({
559+
cvt({
560560
DeviceIoControl(self.handle.raw(),
561561
FSCTL_GET_REPARSE_POINT,
562562
ptr::null_mut(),
@@ -565,7 +565,7 @@ mod win {
565565
space.len() as DWORD,
566566
&mut bytes,
567567
ptr::null_mut())
568-
}));
568+
})?;
569569
Ok((bytes, &*(space.as_ptr() as *const REPARSE_DATA_BUFFER)))
570570
}
571571
}
@@ -792,7 +792,7 @@ mod win {
792792
let mut opts = OpenOptions::new();
793793
opts.access_mode(FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES);
794794
opts.custom_flags(FILE_FLAG_BACKUP_SEMANTICS);
795-
let file = r#try!(File::open(path, &opts));
795+
let file = File::open(path, &opts)?;
796796
file.set_perm(perm)
797797
}
798798

0 commit comments

Comments
 (0)