From 2a6cad455fd655b8fc9adf336294322bebc89e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Thu, 27 Feb 2025 23:08:02 +0800 Subject: [PATCH] refactor: use `MaybeUninit` in `RoCell` (#2412) --- Cargo.lock | 12 ++++----- README.md | 38 ++++++++++++++--------------- cspell.json | 2 +- scripts/validate-form/main.js | 2 +- yazi-shared/src/ro_cell.rs | 46 +++++++++++++++++++++++++---------- 5 files changed, 60 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 21e2c287..b5ee62f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1412,9 +1412,9 @@ checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" [[package]] name = "litemap" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" +checksum = "23fb14cb19457329c82206317a5663005a4d404783dc74f4252769b0d5f42856" [[package]] name = "lock_api" @@ -3684,18 +3684,18 @@ dependencies = [ [[package]] name = "zerofrom" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff3ee08c995dee1859d998dea82f7374f2826091dd9cd47def953cae446cd2e" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" dependencies = [ "zerofrom-derive", ] [[package]] name = "zerofrom-derive" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" dependencies = [ "proc-macro2", "quote", diff --git a/README.md b/README.md index 5ef77d94..c95464c3 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@
Warp, the intelligent terminal
- Yazi's AI-powered terminal of choice!
Available for macOS and Linux
+ Yazi's AI-powered terminal of choice!
Available for macOS, Linux and Windows
@@ -52,24 +52,24 @@ Yazi is currently in heavy development, expect breaking changes. ## Image Preview -| Platform | Protocol | Support | -| ------------------------------------------------------------------------------------------------------------- | -------------------------------------- | ----------------------------------------------------- | -| [kitty](https://github.com/kovidgoyal/kitty) (>= 0.28.0) | [Kitty unicode placeholders][kgp] | ✅ Built-in | -| [iTerm2](https://iterm2.com) | [Inline images protocol][iip] | ✅ Built-in | -| [WezTerm](https://github.com/wez/wezterm) | [Inline images protocol][iip] | ✅ Built-in | -| [Konsole](https://invent.kde.org/utilities/konsole) | [Kitty old protocol][kgp-old] | ✅ Built-in | -| [foot](https://codeberg.org/dnkl/foot) | [Sixel graphics format][sixel] | ✅ Built-in | -| [Ghostty](https://github.com/ghostty-org/ghostty) | [Kitty unicode placeholders][kgp] | ✅ Built-in | -| [Windows Terminal](https://github.com/microsoft/terminal) (>= v1.22.10352.0) | [Sixel graphics format][sixel] | ✅ Built-in | -| [st with Sixel patch](https://github.com/bakkeby/st-flexipatch) | [Sixel graphics format][sixel] | ✅ Built-in | -| [Tabby](https://github.com/Eugeny/tabby) | [Inline images protocol][iip] | ✅ Built-in | -| [VSCode](https://github.com/microsoft/vscode) | [Inline images protocol][iip] | ✅ Built-in | -| [Rio](https://github.com/raphamorim/rio) | [Inline images protocol][iip] | ❌ Rio doesn't correctly clear images [#709][rio-bug] | -| [Black Box](https://gitlab.gnome.org/raggesilver/blackbox) | [Sixel graphics format][sixel] | ✅ Built-in | -| [Hyper](https://github.com/vercel/hyper) | [Inline images protocol][iip] | ✅ Built-in | -| [Bobcat](https://github.com/ismail-yilmaz/Bobcat) | [Inline images protocol][iip] | ✅ Built-in | -| X11 / Wayland | Window system protocol | ☑️ [Überzug++][ueberzug] required | -| Fallback | [ASCII art (Unicode block)][ascii-art] | ☑️ [Chafa][chafa] required | +| Platform | Protocol | Support | +| ---------------------------------------------------------------------------- | -------------------------------------- | ----------------------------------------------------- | +| [kitty](https://github.com/kovidgoyal/kitty) (>= 0.28.0) | [Kitty unicode placeholders][kgp] | ✅ Built-in | +| [iTerm2](https://iterm2.com) | [Inline images protocol][iip] | ✅ Built-in | +| [WezTerm](https://github.com/wez/wezterm) | [Inline images protocol][iip] | ✅ Built-in | +| [Konsole](https://invent.kde.org/utilities/konsole) | [Kitty old protocol][kgp-old] | ✅ Built-in | +| [foot](https://codeberg.org/dnkl/foot) | [Sixel graphics format][sixel] | ✅ Built-in | +| [Ghostty](https://github.com/ghostty-org/ghostty) | [Kitty unicode placeholders][kgp] | ✅ Built-in | +| [Windows Terminal](https://github.com/microsoft/terminal) (>= v1.22.10352.0) | [Sixel graphics format][sixel] | ✅ Built-in | +| [st with Sixel patch](https://github.com/bakkeby/st-flexipatch) | [Sixel graphics format][sixel] | ✅ Built-in | +| [Tabby](https://github.com/Eugeny/tabby) | [Inline images protocol][iip] | ✅ Built-in | +| [VSCode](https://github.com/microsoft/vscode) | [Inline images protocol][iip] | ✅ Built-in | +| [Rio](https://github.com/raphamorim/rio) | [Inline images protocol][iip] | ❌ Rio doesn't correctly clear images [#709][rio-bug] | +| [Black Box](https://gitlab.gnome.org/raggesilver/blackbox) | [Sixel graphics format][sixel] | ✅ Built-in | +| [Hyper](https://github.com/vercel/hyper) | [Inline images protocol][iip] | ✅ Built-in | +| [Bobcat](https://github.com/ismail-yilmaz/Bobcat) | [Inline images protocol][iip] | ✅ Built-in | +| X11 / Wayland | Window system protocol | ☑️ [Überzug++][ueberzug] required | +| Fallback | [ASCII art (Unicode block)][ascii-art] | ☑️ [Chafa][chafa] required | See https://yazi-rs.github.io/docs/image-preview for details. diff --git a/cspell.json b/cspell.json index 131cfa3f..a3b63c4f 100644 --- a/cspell.json +++ b/cspell.json @@ -1 +1 @@ -{"version":"0.2","language":"en","flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","peekable","ratatui","syntect","pbpaste","pbcopy","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","Konsole","Überzug","pkgs","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes","SIGSTOP","sevenzip","rsplitn","replacen","DECSET","DECRQM","repeek","cwds","tcsi","Hyprland","Wayfire","SWAYSOCK","btime","nsec","codegen","gethostname","fchmod","fdfind","Rustc","rustc","ffprobe","vframes","luma","obase","outln","errln","tmtheme","twox","cfgs","fstype","objc","rdev","runloop","exfat","rclone","DECRQSS","DECSCUSR","libvterm"]} \ No newline at end of file +{"words":["Punct","KEYMAP","splitn","crossterm","YAZI","peekable","ratatui","syntect","pbpaste","pbcopy","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","Konsole","Überzug","pkgs","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes","SIGSTOP","sevenzip","rsplitn","replacen","DECSET","DECRQM","repeek","cwds","tcsi","Hyprland","Wayfire","SWAYSOCK","btime","nsec","codegen","gethostname","fchmod","fdfind","Rustc","rustc","ffprobe","vframes","luma","obase","outln","errln","tmtheme","twox","cfgs","fstype","objc","rdev","runloop","exfat","rclone","DECRQSS","DECSCUSR","libvterm","Uninit"],"flagWords":[],"version":"0.2","language":"en"} \ No newline at end of file diff --git a/scripts/validate-form/main.js b/scripts/validate-form/main.js index caf11e3b..cd27c9e2 100644 --- a/scripts/validate-form/main.js +++ b/scripts/validate-form/main.js @@ -1,7 +1,7 @@ const LABEL_NAME = "needs info" const RE_VERSION = /Yazi\s+Version\s*:\s\d+\.\d+\.\d+\s\(/gm const RE_DEPENDENCIES = /Dependencies\s+[/a-z]+\s*:\s/gm -const RE_CHECKLIST = /#{3}\s+Checklist\s+(?:^-\s+\[x]\s+.+?\n){2}/gm +const RE_CHECKLIST = /#{3}\s+Checklist\s+(?:^-\s+\[x]\s+.+?(?:\n|\r\n|$)){2}/gm function bugReportBody(creator, content, hash) { if (content.includes(` (${hash} `) && RE_CHECKLIST.test(content)) { diff --git a/yazi-shared/src/ro_cell.rs b/yazi-shared/src/ro_cell.rs index 2667355b..b5d48f99 100644 --- a/yazi-shared/src/ro_cell.rs +++ b/yazi-shared/src/ro_cell.rs @@ -1,24 +1,41 @@ -use std::{cell::UnsafeCell, fmt::{self, Display}, mem, ops::Deref}; +use std::{cell::UnsafeCell, fmt::{self, Display}, mem::MaybeUninit, ops::Deref}; // Read-only cell. It's safe to use this in a static variable, but it's not safe // to mutate it. This is useful for storing static data that is expensive to // initialize, but is immutable once. -pub struct RoCell(UnsafeCell>); +pub struct RoCell { + inner: UnsafeCell>, + #[cfg(debug_assertions)] + initialized: UnsafeCell, +} unsafe impl Sync for RoCell {} impl RoCell { #[inline] - pub const fn new() -> Self { Self(UnsafeCell::new(None)) } + pub const fn new() -> Self { + Self { + inner: UnsafeCell::new(MaybeUninit::uninit()), + #[cfg(debug_assertions)] + initialized: UnsafeCell::new(false), + } + } #[inline] - pub const fn new_const(value: T) -> Self { Self(UnsafeCell::new(Some(value))) } + pub const fn new_const(value: T) -> Self { + Self { + inner: UnsafeCell::new(MaybeUninit::new(value)), + #[cfg(debug_assertions)] + initialized: UnsafeCell::new(true), + } + } #[inline] pub fn init(&self, value: T) { - debug_assert!(!self.initialized()); unsafe { - *self.0.get() = Some(value); + #[cfg(debug_assertions)] + assert!(!self.initialized.get().replace(true)); + *self.inner.get() = MaybeUninit::new(value); } } @@ -32,12 +49,12 @@ impl RoCell { #[inline] pub fn drop(&self) -> T { - debug_assert!(self.initialized()); - unsafe { mem::take(&mut *self.0.get()).unwrap_unchecked() } + unsafe { + #[cfg(debug_assertions)] + assert!(self.initialized.get().replace(false)); + self.inner.get().replace(MaybeUninit::uninit()).assume_init() + } } - - #[inline] - fn initialized(&self) -> bool { unsafe { (*self.0.get()).is_some() } } } impl Default for RoCell { @@ -48,8 +65,11 @@ impl Deref for RoCell { type Target = T; fn deref(&self) -> &Self::Target { - debug_assert!(self.initialized()); - unsafe { (*self.0.get()).as_ref().unwrap_unchecked() } + unsafe { + #[cfg(debug_assertions)] + assert!(*self.initialized.get()); + (*self.inner.get()).assume_init_ref() + } } }