Skip to content

Commit 9943f73

Browse files
committed
refactor: Unify sys impl under a trait
1 parent 695d88e commit 9943f73

File tree

11 files changed

+954
-895
lines changed

11 files changed

+954
-895
lines changed

src/arena/release.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::ptr::{self, NonNull};
1111
use std::{mem, slice};
1212

1313
use crate::helpers::*;
14+
use crate::sys::Syscall;
1415
use crate::{apperr, sys};
1516

1617
const ALLOC_CHUNK_SIZE: usize = 64 * KIBI;
@@ -66,7 +67,7 @@ impl Arena {
6667

6768
pub fn new(capacity: usize) -> apperr::Result<Self> {
6869
let capacity = (capacity.max(1) + ALLOC_CHUNK_SIZE - 1) & !(ALLOC_CHUNK_SIZE - 1);
69-
let base = unsafe { sys::virtual_reserve(capacity)? };
70+
let base = unsafe { sys::syscall::virtual_reserve(capacity)? };
7071

7172
Ok(Self {
7273
base,
@@ -139,7 +140,8 @@ impl Arena {
139140

140141
if commit_new > self.capacity
141142
|| unsafe {
142-
sys::virtual_commit(self.base.add(commit_old), commit_new - commit_old).is_err()
143+
sys::syscall::virtual_commit(self.base.add(commit_old), commit_new - commit_old)
144+
.is_err()
143145
}
144146
{
145147
return Err(AllocError);
@@ -176,7 +178,7 @@ impl Arena {
176178
impl Drop for Arena {
177179
fn drop(&mut self) {
178180
if !self.is_empty() {
179-
unsafe { sys::virtual_release(self.base, self.capacity) };
181+
unsafe { sys::syscall::virtual_release(self.base, self.capacity) };
180182
}
181183
}
182184
}

src/bin/edit/documents.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::path::{Path, PathBuf};
88

99
use edit::buffer::{RcTextBuffer, TextBuffer};
1010
use edit::helpers::{CoordType, Point};
11+
use edit::sys::Syscall;
1112
use edit::{apperr, path, sys};
1213

1314
use crate::state::DisplayablePathBuf;
@@ -31,7 +32,7 @@ impl Document {
3132
tb.write_file(&mut file)?;
3233
}
3334

34-
if let Ok(id) = sys::file_id(None, path) {
35+
if let Ok(id) = sys::syscall::file_id(None, path) {
3536
self.file_id = Some(id);
3637
}
3738

@@ -51,7 +52,7 @@ impl Document {
5152
tb.read_file(&mut file, encoding)?;
5253
}
5354

54-
if let Ok(id) = sys::file_id(None, path) {
55+
if let Ok(id) = sys::syscall::file_id(None, path) {
5556
self.file_id = Some(id);
5657
}
5758

@@ -145,11 +146,12 @@ impl DocumentManager {
145146

146147
let mut file = match Self::open_for_reading(&path) {
147148
Ok(file) => Some(file),
148-
Err(err) if sys::apperr_is_not_found(err) => None,
149+
Err(err) if sys::syscall::apperr_is_not_found(err) => None,
149150
Err(err) => return Err(err),
150151
};
151152

152-
let file_id = if file.is_some() { Some(sys::file_id(file.as_ref(), &path)?) } else { None };
153+
let file_id =
154+
if file.is_some() { Some(sys::syscall::file_id(file.as_ref(), &path)?) } else { None };
153155

154156
// Check if the file is already open.
155157
if file_id.is_some() && self.update_active(|doc| doc.file_id == file_id) {

src/bin/edit/localization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
use edit::arena::scratch_arena;
55
use edit::helpers::AsciiStringHelpers;
6-
use edit::sys;
6+
use edit::sys::{self, Syscall};
77

88
include!(concat!(env!("OUT_DIR"), "/i18n_edit.rs"));
99

1010
static mut S_LANG: LangId = LangId::en;
1111

1212
pub fn init() {
1313
let scratch = scratch_arena(None);
14-
let langs = sys::preferred_languages(&scratch);
14+
let langs = sys::syscall::preferred_languages(&scratch);
1515
let mut lang = LangId::en;
1616

1717
'outer: for l in langs {

src/bin/edit/main.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use edit::framebuffer::{self, IndexedColor};
2727
use edit::helpers::{CoordType, KIBI, MEBI, MetricFormatter, Rect, Size};
2828
use edit::input::{self, kbmod, vk};
2929
use edit::oklab::StraightRgba;
30+
use edit::sys::Syscall;
3031
use edit::tui::*;
3132
use edit::vt::{self, Token};
3233
use edit::{apperr, arena_format, base64, path, sys, unicode};
@@ -51,15 +52,15 @@ fn main() -> process::ExitCode {
5152
match run() {
5253
Ok(()) => process::ExitCode::SUCCESS,
5354
Err(err) => {
54-
sys::write_stdout(&format!("{}\n", FormatApperr::from(err)));
55+
sys::syscall::write_stdout(&format!("{}\n", FormatApperr::from(err)));
5556
process::ExitCode::FAILURE
5657
}
5758
}
5859
}
5960

6061
fn run() -> apperr::Result<()> {
6162
// Init `sys` first, as everything else may depend on its functionality (IO, function pointers, etc.).
62-
let _sys_deinit = sys::init();
63+
let _sys_deinit = sys::syscall::init();
6364
// Next init `arena`, so that `scratch_arena` works. `loc` depends on it.
6465
arena::init(SCRATCH_ARENA_CAPACITY)?;
6566
// Init the `loc` module, so that error messages are localized.
@@ -75,7 +76,7 @@ fn run() -> apperr::Result<()> {
7576
// `handle_args` may want to print a help message (must not fail),
7677
// and reads files (may hang; should be cancelable with Ctrl+C).
7778
// As such, we call this after `handle_args`.
78-
sys::switch_modes()?;
79+
sys::syscall::switch_modes()?;
7980

8081
let mut vt_parser = vt::Parser::new();
8182
let mut input_parser = input::Parser::new();
@@ -103,7 +104,7 @@ fn run() -> apperr::Result<()> {
103104
tui.set_modal_default_bg(floater_bg);
104105
tui.set_modal_default_fg(floater_fg);
105106

106-
sys::inject_window_size_into_stdin();
107+
sys::syscall::inject_window_size_into_stdin();
107108

108109
#[cfg(feature = "debug-latency")]
109110
let mut last_latency_width = 0;
@@ -118,7 +119,7 @@ fn run() -> apperr::Result<()> {
118119
{
119120
let scratch = scratch_arena(None);
120121
let read_timeout = vt_parser.read_timeout().min(tui.read_timeout());
121-
let Some(input) = sys::read_stdin(&scratch, read_timeout) else {
122+
let Some(input) = sys::syscall::read_stdin(&scratch, read_timeout) else {
122123
break;
123124
};
124125

@@ -214,7 +215,7 @@ fn run() -> apperr::Result<()> {
214215
last_latency_width = cols;
215216
}
216217

217-
sys::write_stdout(&output);
218+
sys::syscall::write_stdout(&output);
218219
}
219220
}
220221

@@ -263,7 +264,7 @@ fn handle_args(state: &mut State) -> apperr::Result<bool> {
263264
cwd = parent.to_path_buf();
264265
}
265266

266-
if let Some(mut file) = sys::open_stdin_if_redirected() {
267+
if let Some(mut file) = sys::syscall::open_stdin_if_redirected() {
267268
let doc = state.documents.add_untitled()?;
268269
let mut tb = doc.buffer.borrow_mut();
269270
tb.read_file(&mut file, None)?;
@@ -278,7 +279,7 @@ fn handle_args(state: &mut State) -> apperr::Result<bool> {
278279
}
279280

280281
fn print_help() {
281-
sys::write_stdout(concat!(
282+
sys::syscall::write_stdout(concat!(
282283
"Usage: edit [OPTIONS] [FILE[:LINE[:COLUMN]]]\n",
283284
"Options:\n",
284285
" -h, --help Print this help message\n",
@@ -290,7 +291,7 @@ fn print_help() {
290291
}
291292

292293
fn print_version() {
293-
sys::write_stdout(concat!("edit version ", env!("CARGO_PKG_VERSION"), "\n"));
294+
sys::syscall::write_stdout(concat!("edit version ", env!("CARGO_PKG_VERSION"), "\n"));
294295
}
295296

296297
fn draw(ctx: &mut Context, state: &mut State) {
@@ -527,12 +528,12 @@ impl Drop for RestoreModes {
527528
// Same as in the beginning but in the reverse order.
528529
// It also includes DECSCUSR 0 to reset the cursor style and DECTCEM to show the cursor.
529530
// We specifically don't reset mode 1036, because most applications expect it to be set nowadays.
530-
sys::write_stdout("\x1b[0 q\x1b[?25h\x1b]0;\x07\x1b[?1002;1006;2004l\x1b[?1049l");
531+
sys::syscall::write_stdout("\x1b[0 q\x1b[?25h\x1b]0;\x07\x1b[?1002;1006;2004l\x1b[?1049l");
531532
}
532533
}
533534

534535
fn setup_terminal(tui: &mut Tui, state: &mut State, vt_parser: &mut vt::Parser) -> RestoreModes {
535-
sys::write_stdout(concat!(
536+
sys::syscall::write_stdout(concat!(
536537
// 1049: Alternative Screen Buffer
537538
// I put the ASB switch in the beginning, just in case the terminal performs
538539
// some additional state tracking beyond the modes we enable/disable.
@@ -570,7 +571,7 @@ fn setup_terminal(tui: &mut Tui, state: &mut State, vt_parser: &mut vt::Parser)
570571
// We explicitly set a high read timeout, because we're not
571572
// waiting for user keyboard input. If we encounter a lone ESC,
572573
// it's unlikely to be from a ESC keypress, but rather from a VT sequence.
573-
let Some(input) = sys::read_stdin(&scratch, Duration::from_secs(3)) else {
574+
let Some(input) = sys::syscall::read_stdin(&scratch, Duration::from_secs(3)) else {
574575
break;
575576
};
576577

src/bin/edit/state.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::path::{Path, PathBuf};
99
use edit::framebuffer::IndexedColor;
1010
use edit::helpers::*;
1111
use edit::oklab::StraightRgba;
12+
use edit::sys::Syscall;
1213
use edit::tui::*;
1314
use edit::{apperr, buffer, icu, sys};
1415

@@ -30,7 +31,7 @@ impl std::fmt::Display for FormatApperr {
3031
apperr::APP_ICU_MISSING => f.write_str(loc(LocId::ErrorIcuMissing)),
3132
apperr::Error::App(code) => write!(f, "Unknown app error code: {code}"),
3233
apperr::Error::Icu(code) => icu::apperr_format(f, code),
33-
apperr::Error::Sys(code) => sys::apperr_format(f, code),
34+
apperr::Error::Sys(code) => sys::syscall::apperr_format(f, code),
3435
}
3536
}
3637
}

src/buffer/gap_buffer.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::slice;
77

88
use crate::document::{ReadableDocument, WriteableDocument};
99
use crate::helpers::*;
10+
use crate::sys::Syscall;
1011
use crate::{apperr, sys};
1112

1213
#[cfg(target_pointer_width = "32")]
@@ -31,7 +32,7 @@ impl Drop for BackingBuffer {
3132
fn drop(&mut self) {
3233
unsafe {
3334
if let Self::VirtualMemory(ptr, reserve) = *self {
34-
sys::virtual_release(ptr, reserve);
35+
sys::syscall::virtual_release(ptr, reserve);
3536
}
3637
}
3738
}
@@ -73,7 +74,7 @@ impl GapBuffer {
7374
buffer = BackingBuffer::Vec(Vec::new());
7475
} else {
7576
reserve = LARGE_CAPACITY;
76-
text = unsafe { sys::virtual_reserve(reserve)? };
77+
text = unsafe { sys::syscall::virtual_reserve(reserve)? };
7778
buffer = BackingBuffer::VirtualMemory(text, reserve);
7879
}
7980

@@ -195,7 +196,9 @@ impl GapBuffer {
195196

196197
match &mut self.buffer {
197198
BackingBuffer::VirtualMemory(ptr, _) => unsafe {
198-
if sys::virtual_commit(ptr.add(bytes_old), bytes_new - bytes_old).is_err() {
199+
if sys::syscall::virtual_commit(ptr.add(bytes_old), bytes_new - bytes_old)
200+
.is_err()
201+
{
199202
return;
200203
}
201204
},

src/icu.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::ptr::{null, null_mut};
1212

1313
use crate::arena::{Arena, ArenaString, scratch_arena};
1414
use crate::buffer::TextBuffer;
15+
use crate::sys::Syscall;
1516
use crate::unicode::Utf8Chars;
1617
use crate::{apperr, arena_format, sys};
1718

@@ -978,7 +979,7 @@ fn init_if_needed() -> apperr::Result<&'static LibraryFunctions> {
978979
unsafe {
979980
LIBRARY_FUNCTIONS = LibraryFunctionsState::Failed;
980981

981-
let Ok(icu) = sys::load_icu() else {
982+
let Ok(icu) = sys::syscall::load_icu() else {
982983
return;
983984
};
984985

@@ -1017,7 +1018,7 @@ fn init_if_needed() -> apperr::Result<&'static LibraryFunctions> {
10171018
#[cfg(edit_icu_renaming_auto_detect)]
10181019
let name = sys::icu_add_renaming_suffix(&scratch, name, &suffix);
10191020

1020-
let Ok(func) = sys::get_proc_address(handle, name) else {
1021+
let Ok(func) = sys::syscall::get_proc_address(handle, name) else {
10211022
debug_assert!(
10221023
false,
10231024
"Failed to load ICU function: {:?}",

src/simd/memchr2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ mod tests {
226226
use std::slice;
227227

228228
use super::*;
229-
use crate::sys;
229+
use crate::sys::{self, Syscall};
230230

231231
#[test]
232232
fn test_empty() {
@@ -265,8 +265,8 @@ mod tests {
265265
const PAGE_SIZE: usize = 64 * 1024; // 64 KiB to cover many architectures.
266266

267267
// 3 pages: uncommitted, committed, uncommitted
268-
let ptr = sys::virtual_reserve(PAGE_SIZE * 3).unwrap();
269-
sys::virtual_commit(ptr.add(PAGE_SIZE), PAGE_SIZE).unwrap();
268+
let ptr = sys::syscall::virtual_reserve(PAGE_SIZE * 3).unwrap();
269+
sys::syscall::virtual_commit(ptr.add(PAGE_SIZE), PAGE_SIZE).unwrap();
270270
slice::from_raw_parts_mut(ptr.add(PAGE_SIZE).as_ptr(), PAGE_SIZE)
271271
};
272272

0 commit comments

Comments
 (0)