From 5cbaf2ae11950eb5daf56e4f8d9c4120e3f62a2a Mon Sep 17 00:00:00 2001 From: LoveSy Date: Fri, 22 Aug 2025 13:16:09 +0800 Subject: [PATCH] Use super let to simplify code --- native/src/boot/compress.rs | 68 +++++++++++++++++-------------------- native/src/boot/lib.rs | 1 + 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/native/src/boot/compress.rs b/native/src/boot/compress.rs index 321af2c73..e0ad1b5fc 100644 --- a/native/src/boot/compress.rs +++ b/native/src/boot/compress.rs @@ -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()); diff --git a/native/src/boot/lib.rs b/native/src/boot/lib.rs index fb98f7d92..6f1d72667 100644 --- a/native/src/boot/lib.rs +++ b/native/src/boot/lib.rs @@ -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};