Skip to content

Commit

Permalink
Migrate more crates to rustix (#2016)
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe authored Jan 18, 2025
1 parent 6895042 commit e8f4cc4
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 40 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions cmd/minimal/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/minimal/interpret/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ readme.workspace = true
repository.workspace = true

[dependencies]
libc = { version = "0.2", default-features = false }
stak-util = { version = "0.1.18", path = "../../../util" }
libc = { version = "0.2.169", default-features = false }
stak-device = { version = "0.2.24", path = "../../../device", features = [
"libc",
] }
Expand All @@ -20,6 +19,7 @@ stak-process-context = { version = "0.2.0", path = "../../../process_context", f
] }
stak-r7rs = { version = "0.9.0", path = "../../../r7rs" }
stak-time = { version = "0.1.0", path = "../../../time", features = ["libc"] }
stak-util = { version = "0.1.18", path = "../../../util" }
stak-vm = { version = "0.7.0", path = "../../../vm" }

[lints]
Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"fseek",
"fstat",
"ftell",
"gettime",
"heapless",
"imag",
"indoc",
Expand Down
6 changes: 4 additions & 2 deletions device/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ readme.workspace = true
repository.workspace = true

[features]
libc = ["dep:libc"]
libc = ["dep:rustix"]
std = []

[dependencies]
libc = { version = "0.2", default-features = false, optional = true }
rustix = { version = "0.38.43", default-features = false, features = [
"stdio",
], optional = true }
stak-vm = { version = "0.7.21", path = "../vm" }

[lints]
Expand Down
2 changes: 1 addition & 1 deletion device/src/device/libc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{
};

/// An error.
#[derive(Debug)]
#[derive(Clone, Copy, Debug)]
pub enum LibcError {
/// A stdin error.
Stdin,
Expand Down
40 changes: 33 additions & 7 deletions device/src/device/libc/stdio.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use super::{error::LibcError, Read, Write};
use rustix::{
fd::BorrowedFd,
io,
stdio::{stderr, stdin, stdout},
};

/// A stdin.
#[derive(Debug, Default)]
Expand All @@ -18,7 +23,16 @@ impl Read for Stdin {
let mut bytes = [0];

Ok(
if unsafe { libc::read(libc::STDIN_FILENO, &mut bytes as *mut _ as _, 1) } == 1 {
if io::read(
#[allow(unused_unsafe)]
unsafe {
stdin()
},
&mut bytes,
)
.map_err(|_| LibcError::Stdin)?
== 1
{
Some(bytes[0])
} else {
None
Expand All @@ -42,7 +56,14 @@ impl Write for Stdout {
type Error = LibcError;

fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
write(libc::STDOUT_FILENO, byte, LibcError::Stdout)
write(
#[allow(unused_unsafe)]
unsafe {
stdout()
},
byte,
LibcError::Stdout,
)
}
}

Expand All @@ -61,14 +82,19 @@ impl Write for Stderr {
type Error = LibcError;

fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
write(libc::STDERR_FILENO, byte, LibcError::Stderr)
write(
#[allow(unused_unsafe)]
unsafe {
stderr()
},
byte,
LibcError::Stderr,
)
}
}

fn write(fd: i32, byte: u8, error: LibcError) -> Result<(), LibcError> {
let mut bytes = [byte];

if unsafe { libc::write(fd, &mut bytes as *mut _ as _, 1) } == 1 {
fn write(fd: BorrowedFd, byte: u8, error: LibcError) -> Result<(), LibcError> {
if io::write(fd, &[byte]).map_err(|_| error)? == 1 {
Ok(())
} else {
Err(error)
Expand Down
2 changes: 1 addition & 1 deletion sac/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ std = [

[dependencies]
clap = { version = "4.5.26", features = ["derive"], optional = true }
libc = { version = "0.2", default-features = false, optional = true }
libc = { version = "0.2.169", default-features = false, optional = true }
main_error = { version = "0.1.2", optional = true }
stak-configuration = { version = "0.1.88", path = "../configuration" }
stak-device = { version = "0.2.92", path = "../device" }
Expand Down
6 changes: 4 additions & 2 deletions time/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ readme.workspace = true
repository.workspace = true

[features]
libc = ["dep:libc"]
libc = ["dep:rustix"]
std = []

[dependencies]
libc = { version = "0.2", default-features = false, optional = true }
rustix = { version = "0.38.43", default-features = false, features = [
"time",
], optional = true }
stak-vm = { version = "0.7.21", path = "../vm" }

[lints]
Expand Down
10 changes: 6 additions & 4 deletions time/src/clock/libc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Clock;
use core::{convert::Infallible, ptr::null_mut};
use core::convert::Infallible;
use rustix::time::{clock_gettime, ClockId};
use stak_vm::Number;

/// A clock based on libc.
Expand All @@ -17,8 +18,9 @@ impl Clock for LibcClock {
type Error = Infallible;

fn current_jiffy(&self) -> Result<Number, Self::Error> {
Ok(Number::from_i64(
unsafe { libc::time(null_mut()) } * 1_000_000_000,
))
let time = clock_gettime(ClockId::Realtime);

// spell-checker: disable-next-line
Ok(Number::from_i64(time.tv_sec * 1_000_000_000 + time.tv_nsec))
}
}
6 changes: 5 additions & 1 deletion util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ readme.workspace = true
repository.workspace = true

[dependencies]
libc = { version = "0.2", default-features = false }
libc = { version = "0.2.169", default-features = false }
rustix = { version = "0.38.43", default-features = false, features = [
"fs",
"mm",
] }

[lints]
workspace = true
2 changes: 0 additions & 2 deletions util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

mod heap;
mod mmap;
mod validate;

use core::ffi::CStr;
pub use heap::Heap;
pub use mmap::Mmap;
pub use validate::validate;

/// Reads a file size at a path.
pub fn read_file_size(path: &CStr) -> usize {
Expand Down
17 changes: 11 additions & 6 deletions util/src/mmap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::{read_file_size, validate};
use crate::read_file_size;
use core::{ffi::CStr, ptr::null_mut, slice};
use rustix::{
fs::{self, Mode, OFlags},
mm::{mmap, munmap, MapFlags, ProtFlags},
};

/// A mmap.
pub struct Mmap {
Expand All @@ -14,15 +18,16 @@ impl Mmap {

Self {
ptr: unsafe {
libc::mmap(
mmap(
null_mut(),
len,
libc::PROT_READ,
libc::MAP_PRIVATE,
ProtFlags::READ,
MapFlags::PRIVATE,
// spell-checker: disable-next-line
libc::open(path.as_ptr(), libc::O_RDONLY),
fs::open(path, OFlags::RDONLY, Mode::RUSR).unwrap(),
0,
)
.unwrap()
} as _,
len,
}
Expand All @@ -37,7 +42,7 @@ impl Mmap {
impl Drop for Mmap {
fn drop(&mut self) {
unsafe {
validate(libc::munmap(self.ptr as _, self.len));
munmap(self.ptr as _, self.len).unwrap();
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions util/src/validate.rs

This file was deleted.

0 comments on commit e8f4cc4

Please sign in to comment.