Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Version 0.1.1: add os_str_bytes feature
Browse files Browse the repository at this point in the history
  • Loading branch information
LoganDark committed Jun 27, 2023
1 parent 9f6ad34 commit 9c1ac25
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = 'getargs-os'
version = '0.1.0'
version = '0.1.1'
authors = [
'LoganDark <contact@logandark.mozmail.com>',
'Cuteness! <contact@logandark.mozmail.com>' # We think cuteness contributed code here
Expand All @@ -13,11 +13,13 @@ readme = './README.md'
homepage = 'https://github.com/LoganDark/getargs-os'
repository = 'https://github.com/LoganDark/getargs-os'
license = 'MIT'
license-file = './LICENSE.md'
keywords = ['getargs', 'argv', 'arg', 'argument', 'parser']
categories = ['command-line-interface', 'rust-patterns']
exclude = ['.idea']

[features]
os_str_bytes = []

[dependencies]
getargs = '~0.5.0'

Expand Down
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(feature = "os_str_bytes", feature(os_str_bytes))]

#![allow(clippy::tabs_in_doc_comments)] // what a stupid fucking lint

//! Adds a newtype wrapper ([`OsArgument`]) around [`OsStr`] that allows it to
Expand Down Expand Up @@ -45,6 +47,16 @@
//! # }
//! ```
//!
//! ### `os_str_bytes` feature
//!
//! To unlock `From<&str>` and `PartialEq<&str>` impls for `&OsArgument`, you
//! must enable the unstable `os_str_bytes` feature, which depends on Nightly.
//! This is because earlier versions of Rust didn't provide guarantees that OS
//! strings are a superset of UTF-8 (even though `getargs-os` relied on this
//! anyway in the past). Since the feature now exists, I don't want to make
//! `getargs-os` unconditionally require Nightly, but new features relying on
//! this guarantee will be gated behind the `os_str_bytes` feature until it is
//! stabilized.
use std::ffi::OsStr;
use std::fmt::{Debug, Formatter};
Expand Down Expand Up @@ -79,6 +91,13 @@ impl<'a> From<&'a OsArgument> for &'a OsStr {
}
}

#[cfg(feature = "os_str_bytes")]
impl From<&str> for &OsArgument {
fn from(from: &str) -> Self {
Self::from(unsafe { OsStr::from_os_str_bytes_unchecked(from.as_bytes()) })
}
}

impl OsArgument {
fn as_bytes(&self) -> &[u8] {
#[cfg(windows)]
Expand Down Expand Up @@ -117,6 +136,20 @@ impl PartialEq for OsArgument {
fn eq(&self, other: &Self) -> bool { self.0 == other.0 }
}

#[cfg(feature = "os_str_bytes")]
impl PartialEq<&str> for &OsArgument {
fn eq(&self, other: &str) -> bool {
self == other.into()
}
}

#[cfg(feature = "os_str_bytes")]
impl PartialEq<&OsArgument> for &str {
fn eq(&self, other: &OsArgument) -> bool {
self.into() == other
}
}

impl Eq for OsArgument {}

impl Debug for OsArgument {
Expand Down

0 comments on commit 9c1ac25

Please sign in to comment.