Skip to content

Commit

Permalink
Fixup some missing parts for major
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusGrass committed Oct 1, 2023
1 parent 04c03b9 commit 1178f92
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ for applications that can be threaded.
14. [ ] Use more efficient syscall semantics, i.e. `eax` over `rax` if the return-value isn't register size.
15. [ ] Use type-checked builders as args for comptime error evaluation of syscalls.
16. [ ] Enforce correct features for symbol relocation through a build-script (Fail compilation with
static relocation if `aux` feature isn't enabled, since that will result in a botched binary).
static relocation if `aux` feature isn't enabled, since that will result in a botched binary).
17. [ ] Path operations on &UnixStr
18. [x] Implement `from_str` on &UnixStr
19. [x] Throw a rusl::Error instead of Utf8Error on `as_str`
2 changes: 1 addition & 1 deletion rusl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rusl"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
license = "MPL-2.0"
readme = "../Readme.md"
Expand Down
5 changes: 5 additions & 0 deletions rusl/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

## [v0.2.1] - 2023-10-01

### Changed
- Throw a rusl error instead of a Utf8Error on failed `UnixStr` conversions.

## [v0.2.0] - 2023-10-01

### Changed
Expand Down
41 changes: 36 additions & 5 deletions rusl/src/string/unix_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@ use crate::Error;
use alloc::string::String;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::fmt::{Debug, Formatter};
use core::hash::Hasher;
use core::str::Utf8Error;

use crate::platform::NULL_BYTE;
use crate::string::strlen::strlen;

#[cfg(feature = "alloc")]
#[repr(transparent)]
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct UnixString(pub(crate) Vec<u8>);

#[cfg(feature = "alloc")]
impl Debug for UnixString {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let slice = unsafe { core::slice::from_raw_parts(self.0.as_ptr(), self.0.len()) };
match core::str::from_utf8(slice) {
Ok(raw) => f.write_fmt(format_args!("UnixString({raw})")),
Err(_e) => f.write_fmt(format_args!("UnixString({slice:?})")),
}
}
}

#[cfg(feature = "alloc")]
impl UnixString {
#[inline]
Expand Down Expand Up @@ -97,7 +108,7 @@ impl AsRef<UnixStr> for UnixString {
}

#[repr(transparent)]
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Eq, PartialEq, Ord, PartialOrd)]
pub struct UnixStr(pub(crate) [u8]);

impl UnixStr {
Expand Down Expand Up @@ -168,9 +179,9 @@ impl UnixStr {
/// Try to convert this `&UnixStr` to a utf8 `&str`
/// # Errors
/// Not utf8
pub fn as_str(&self) -> Result<&str, Utf8Error> {
pub fn as_str(&self) -> Result<&str, Error> {
let slice = unsafe { core::slice::from_raw_parts(self.0.as_ptr(), self.0.len() - 1) };
core::str::from_utf8(slice)
Ok(core::str::from_utf8(slice)?)
}

/// Get this `&UnixStr` as a slice, including the null byte
Expand Down Expand Up @@ -238,6 +249,16 @@ impl UnixStr {
}
}

impl<'a> Debug for &'a UnixStr {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let slice = unsafe { core::slice::from_raw_parts(self.0.as_ptr(), self.0.len()) };
match core::str::from_utf8(slice) {
Ok(inner) => f.write_fmt(format_args!("UnixStr({inner})")),
Err(_e) => f.write_fmt(format_args!("UnixStr({slice:?})")),
}
}
}

impl<'a> core::hash::Hash for &'a UnixStr {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
Expand All @@ -253,6 +274,16 @@ impl From<&UnixStr> for UnixString {
}
}

#[cfg(feature = "alloc")]
impl core::str::FromStr for UnixString {
type Err = Error;

#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from_str(s)
}
}

#[inline]
const fn const_null_term_validate(s: &[u8]) {
assert!(
Expand Down
4 changes: 2 additions & 2 deletions test-runners/alloc-st-main/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 test-runners/no-alloc-main/Cargo.lock

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

2 changes: 1 addition & 1 deletion test-runners/test-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn run_minimal_feature_set() {
fn get_env() {
let v_unix = tiny_std::env::var_unix(UnixStr::try_from_str("HOME\0").unwrap()).unwrap();
let v = tiny_std::env::var("HOME").unwrap();
assert_eq!(v, v_unix);
assert_eq!(v, v_unix.as_str().unwrap());
if is_ci() {
assert_eq!("/home/runner", v);
} else {
Expand Down
4 changes: 2 additions & 2 deletions test-runners/threaded-main/Cargo.lock

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

2 changes: 1 addition & 1 deletion tiny-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tiny-std"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
license = "MPL-2.0"
readme = "../Readme.md"
Expand Down
5 changes: 5 additions & 0 deletions tiny-std/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

## [v0.2.1] - 2023-10-01

### Changed
- Expose errors at crate root

## [v0.2.0] - 2023-10-01
### Fixed
- Create dir all used to error when it ended with a slash
Expand Down
14 changes: 7 additions & 7 deletions tiny-std/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::str::Utf8Error;

use crate::error::Error;
use rusl::string::strlen::strlen;
use rusl::string::unix_str::UnixStr;

Expand Down Expand Up @@ -27,7 +28,7 @@ pub enum VarError {
/// # Errors
/// 1. Value is not in the environment
/// 2. Value exists but is not utf-8
pub fn var_unix(key: &UnixStr) -> Result<&'static str, VarError> {
pub fn var_unix(key: &UnixStr) -> Result<&'static UnixStr, VarError> {
let mut env_ptr = unsafe { ENV.env_p };
while !env_ptr.is_null() {
unsafe {
Expand All @@ -40,10 +41,9 @@ pub fn var_unix(key: &UnixStr) -> Result<&'static str, VarError> {
if match_up_to != 0 {
// Next is '='
if var_ptr.add(match_up_to).read() == b'=' {
let value_len = strlen(var_ptr.add(match_up_to + 1));
let value_slice =
core::slice::from_raw_parts(var_ptr.add(match_up_to + 1), value_len);
return core::str::from_utf8(value_slice).map_err(VarError::NotUnicode);
// # Safety
// Trusting the OS to null terminate
return Ok(UnixStr::from_ptr(var_ptr.add(match_up_to + 1)));
}
}

Expand Down Expand Up @@ -103,11 +103,11 @@ pub fn args_os() -> ArgsOs {
pub struct Args(ArgsOs);

impl Iterator for Args {
type Item = Result<&'static str, Utf8Error>;
type Item = Result<&'static str, Error>;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(UnixStr::as_str)
self.0.next().map(|e| Ok(UnixStr::as_str(e)?))
}
}

Expand Down
2 changes: 1 addition & 1 deletion tiny-std/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt::{Debug, Display, Formatter};

pub use rusl::error::Errno;
use rusl::error::Errno;

pub type Result<T> = core::result::Result<T, Error>;

Expand Down
5 changes: 4 additions & 1 deletion tiny-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
#[cfg(feature = "alloc")]
extern crate alloc;

pub use error::{Error, Result};
pub use rusl::error::Errno;
pub use rusl::string::unix_str::*;
pub use rusl::Error as RuslError;

#[cfg(feature = "allocator-provided")]
pub mod allocator;
pub mod elf;
#[cfg(feature = "start")]
pub mod env;
pub mod error;
mod error;
pub mod fs;
pub mod io;
pub mod linux;
Expand Down

0 comments on commit 1178f92

Please sign in to comment.