Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve no-std support #1

Merged
merged 3 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ readme = "README.md"
license = "MIT"
description = "An unsafe reference without explicit lifetime"
keywords = []
categories = []
categories = ["rust-patterns", "no-std", "no-std::no-alloc"]

[features]
default = ["alloc"]
alloc = []
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
An unsafe reference without explicit lifetime
</div>

<br>

<div align="right">
<a href="https://github.com/ohkami-rs/unsaferef/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/crates/l/unsaferef.svg" /></a>
<a href="https://github.com/ohkami-rs/unsaferef/actions"><img alt="CI status" src="https://github.com/ohkami-rs/unsaferef/actions/workflows/CI.yml/badge.svg"/></a>
Expand All @@ -30,6 +28,12 @@ fn main() {
}
```

## no-std and cow

`UnsafeRef` supports `no-std::no-alloc`.

On `alloc` feature ( default ), unsaferef additionally provides `UnsafeCow`, an unsafe Clone-on-Write container without explicit lifetime.

## License

unsaferef is licensed under MIT LICENSE ( [LICENSE](https://github.com/ohkami-rs/unsaferef/blob/main/LICENSE) or [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT) ).
8 changes: 5 additions & 3 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ tasks:
test:doc:
cmds:
- cargo test --doc

test:lib:
cmds:
- cargo test --lib
- cargo test --lib --no-default-features
- cargo test --lib --features alloc

##### check #####
check:lib:
cmds:
- cargo check
- cargo check --no-default-features
- cargo check --features alloc
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
mod test_readme {}

mod unsafe_ref;
mod unsafe_cow;

pub use unsafe_ref::UnsafeRef;

#[cfg(feature="alloc")]
mod unsafe_cow;
#[cfg(feature="alloc")]
pub use unsafe_cow::UnsafeCow;
2 changes: 2 additions & 0 deletions src/unsafe_cow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature="alloc")]

use crate::UnsafeRef;

pub enum UnsafeCow<T: ?Sized + ToOwned> {
Expand Down
18 changes: 9 additions & 9 deletions src/unsafe_ref.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ptr::NonNull;
use core::ptr::NonNull;

pub struct UnsafeRef<T: ?Sized>(NonNull<T>);

Expand All @@ -20,7 +20,7 @@ impl<T: ?Sized> Clone for UnsafeRef<T> {
}
impl<T: ?Sized> Copy for UnsafeRef<T> {}

impl<T: ?Sized> std::ops::Deref for UnsafeRef<T> {
impl<T: ?Sized> core::ops::Deref for UnsafeRef<T> {
type Target = T;

#[inline(always)]
Expand All @@ -30,24 +30,24 @@ impl<T: ?Sized> std::ops::Deref for UnsafeRef<T> {
}
}

impl<T: ?Sized + std::hash::Hash> std::hash::Hash for UnsafeRef<T> {
impl<T: ?Sized + core::hash::Hash> core::hash::Hash for UnsafeRef<T> {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
(&**self).hash(state);
}
}

impl<T: ?Sized + std::fmt::Debug> std::fmt::Debug for UnsafeRef<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl<T: ?Sized + core::fmt::Debug> core::fmt::Debug for UnsafeRef<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("UnsafeRef")
.field(&&**self)
.finish()
}
}
impl<T: ?Sized + std::fmt::Display> std::fmt::Display for UnsafeRef<T> {
impl<T: ?Sized + core::fmt::Display> core::fmt::Display for UnsafeRef<T> {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&**self, f)
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(&**self, f)
}
}

Expand Down
Loading