Skip to content

Commit d5cde80

Browse files
authored
Use Result for write_to_fd return value (fish-shell#10308)
1 parent e6687dc commit d5cde80

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/env_universal_common.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::collections::hash_map::Entry;
2323
use std::collections::HashSet;
2424
use std::ffi::CString;
2525
use std::mem::MaybeUninit;
26-
use std::os::fd::{AsRawFd, OwnedFd, RawFd};
26+
use std::os::fd::{AsFd, AsRawFd, OwnedFd, RawFd};
2727
use std::os::unix::prelude::MetadataExt;
2828

2929
// Pull in the O_EXLOCK constant if it is defined, otherwise set it to 0.
@@ -533,11 +533,12 @@ impl EnvUniversal {
533533
}
534534

535535
/// Writes our state to the fd. path is provided only for error reporting.
536-
fn write_to_fd(&mut self, fd: RawFd, path: &wstr) -> bool {
537-
assert!(fd >= 0);
538-
let mut success = true;
536+
fn write_to_fd(&mut self, fd: impl AsFd, path: &wstr) -> std::io::Result<usize> {
537+
let fd = fd.as_fd();
539538
let contents = Self::serialize_with_vars(&self.vars);
540-
if let Err(err) = write_loop(&fd, &contents) {
539+
let res = write_loop(&fd, &contents);
540+
541+
if let Err(err) = res.as_ref() {
541542
let error = Errno(err.raw_os_error().unwrap());
542543
FLOG!(
543544
error,
@@ -547,14 +548,13 @@ impl EnvUniversal {
547548
error.to_string()
548549
),
549550
);
550-
success = false;
551551
}
552552

553553
// Since we just wrote out this file, it matches our internal state; pretend we read from it.
554-
self.last_read_file = file_id_for_fd(fd);
554+
self.last_read_file = file_id_for_fd(fd.as_raw_fd());
555555

556556
// We don't close the file.
557-
success
557+
res
558558
}
559559

560560
fn move_new_vars_file_into_place(&mut self, src: &wstr, dst: &wstr) -> bool {
@@ -768,7 +768,7 @@ impl EnvUniversal {
768768
let private_file_path = &delete_pfp;
769769

770770
// Write to it.
771-
if !self.write_to_fd(private_fd.as_raw_fd(), &private_file_path) {
771+
if self.write_to_fd(&private_fd, private_file_path).is_err() {
772772
FLOG!(uvar_file, "universal log write_to_fd() failed");
773773
return false;
774774
}
@@ -812,7 +812,7 @@ impl EnvUniversal {
812812
}
813813

814814
// Apply new file.
815-
if !self.move_new_vars_file_into_place(&private_file_path, &real_path) {
815+
if !self.move_new_vars_file_into_place(private_file_path, &real_path) {
816816
FLOG!(
817817
uvar_file,
818818
"universal log move_new_vars_file_into_place() failed"

0 commit comments

Comments
 (0)