Use super let to simplify code

This commit is contained in:
LoveSy
2025-08-22 13:16:09 +08:00
committed by John Wu
parent 8ebc6207b4
commit 5cbaf2ae11
2 changed files with 32 additions and 37 deletions

View File

@@ -10,11 +10,11 @@ use lz4::{
};
use std::cell::Cell;
use std::fs::File;
use std::io::{BufWriter, Read, Stdin, Stdout, Write, stdin, stdout};
use std::io::{BufWriter, Read, Write, stdin, stdout};
use std::mem::ManuallyDrop;
use std::num::NonZeroU64;
use std::ops::DerefMut;
use std::os::fd::{AsRawFd, FromRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, FromRawFd, RawFd};
use xz2::{
stream::{Check as LzmaCheck, Filters as LzmaFilters, LzmaOptions, Stream as LzmaStream},
write::{XzDecoder, XzEncoder},
@@ -426,36 +426,20 @@ pub fn decompress_bytes(format: FileFormat, in_bytes: &[u8], out_fd: RawFd) {
};
}
enum AsRawFdFile {
Stdin(Stdin),
Stdout(Stdout),
File(File),
}
impl AsRawFd for AsRawFdFile {
fn as_raw_fd(&self) -> RawFd {
match self {
AsRawFdFile::Stdin(stdin) => stdin.as_raw_fd(),
AsRawFdFile::Stdout(stdout) => stdout.as_raw_fd(),
AsRawFdFile::File(file) => file.as_raw_fd(),
}
}
}
pub(crate) fn decompress(infile: &mut String, outfile: Option<&mut String>) -> LoggedResult<()> {
let in_std = infile == "-";
let mut rm_in = false;
let raw_in = if in_std {
AsRawFdFile::Stdin(stdin())
} else {
AsRawFdFile::File(Utf8CStr::from_string(infile).open(O_RDONLY)?)
};
let mut buf = [0u8; 4096];
let mut in_file = unsafe { File::from_raw_fd(raw_in.as_raw_fd()) };
let _ = in_file.read(&mut buf)?;
let raw_in = if in_std {
super let mut stdin = stdin();
let _ = stdin.read(&mut buf)?;
stdin.as_fd()
} else {
super let mut infile = Utf8CStr::from_string(infile).open(O_RDONLY)?;
let _ = infile.read(&mut buf)?;
infile.as_fd()
};
let format = check_fmt(&buf);
@@ -467,12 +451,15 @@ pub(crate) fn decompress(infile: &mut String, outfile: Option<&mut String>) -> L
let raw_out = if let Some(outfile) = outfile {
if outfile == "-" {
AsRawFdFile::Stdout(stdout())
super let stdout = stdout();
stdout.as_fd()
} else {
AsRawFdFile::File(Utf8CStr::from_string(outfile).create(O_WRONLY | O_TRUNC, 0o644)?)
super let outfile = Utf8CStr::from_string(outfile).create(O_WRONLY | O_TRUNC, 0o644)?;
outfile.as_fd()
}
} else if in_std {
AsRawFdFile::Stdout(stdout())
super let stdout = stdout();
stdout.as_fd()
} else {
// strip the extension
rm_in = true;
@@ -486,7 +473,8 @@ pub(crate) fn decompress(infile: &mut String, outfile: Option<&mut String>) -> L
};
eprintln!("Decompressing to [{outfile}]");
AsRawFdFile::File(Utf8CStr::from_string(&mut outfile).create(O_WRONLY | O_TRUNC, 0o644)?)
super let outfile = Utf8CStr::from_string(&mut outfile).create(O_WRONLY | O_TRUNC, 0o644)?;
outfile.as_fd()
};
decompress_bytes_fd(format, &buf, raw_in.as_raw_fd(), raw_out.as_raw_fd());
@@ -511,24 +499,30 @@ pub(crate) fn compress(
let mut rm_in = false;
let raw_in = if in_std {
AsRawFdFile::Stdin(stdin())
super let stdin = stdin();
stdin.as_fd()
} else {
AsRawFdFile::File(Utf8CStr::from_string(infile).open(O_RDONLY)?)
super let infile = Utf8CStr::from_string(infile).open(O_RDONLY)?;
infile.as_fd()
};
let raw_out = if let Some(outfile) = outfile {
if outfile == "-" {
AsRawFdFile::Stdout(stdout())
super let stdout = stdout();
stdout.as_fd()
} else {
AsRawFdFile::File(Utf8CStr::from_string(outfile).create(O_WRONLY | O_TRUNC, 0o644)?)
super let outfile = Utf8CStr::from_string(outfile).create(O_WRONLY | O_TRUNC, 0o644)?;
outfile.as_fd()
}
} else if in_std {
AsRawFdFile::Stdout(stdout())
super let stdout = stdout();
stdout.as_fd()
} else {
let mut outfile = format!("{infile}.{}", method.ext());
eprintln!("Compressing to [{outfile}]");
rm_in = true;
AsRawFdFile::File(Utf8CStr::from_string(&mut outfile).create(O_WRONLY | O_TRUNC, 0o644)?)
super let outfile = Utf8CStr::from_string(&mut outfile).create(O_WRONLY | O_TRUNC, 0o644)?;
outfile.as_fd()
};
compress_fd(method, raw_in.as_raw_fd(), raw_out.as_raw_fd());

View File

@@ -2,6 +2,7 @@
#![feature(btree_extract_if)]
#![feature(iter_intersperse)]
#![feature(try_blocks)]
#![feature(super_let)]
pub use base;
use compress::{compress_bytes, decompress_bytes};