Skip to content

Commit

Permalink
uhm, put back ptr fns and something?
Browse files Browse the repository at this point in the history
still, not sure if I like this lol, but it's, something
  • Loading branch information
meadowsys committed Jul 22, 2024
1 parent 3640d29 commit d6c42a2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 39 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ zeroize = { version = "1.7.0", optional = true }

[features]
all = ["augment-panic-hook", "clock-timer", "debounce", "export-all-submodules", "h", "hex", "lazy-wrap", "nominal", "rand", "with-cloned", "z85"]
all-unstable = ["all", "aoc-unstable", "auth-unstable", "bitstream-unstable", "chainer-unstable", "cli-unstable", "defer-unstable", "id-unstable", "int-unstable", "iter-unstable", "lsl-unstable", "mcu-unstable", "memory-usage-unstable", "minesweeper-unstable", "num-traits-unstable", "path-unstable", "serialiser-binary-unstable", "serialiser-text-unstable", "string-pool-unstable", "sudoku-unstable", "unicode-unstable"]
all-unstable = ["all", "aoc-unstable", "auth-unstable", "bitstream-unstable", "chainer-unstable", "cli-unstable", "defer-unstable", "id-unstable", "int-unstable", "iter-unstable", "lsl-unstable", "mcu-unstable", "memory-usage-unstable", "minesweeper-unstable", "num-traits-unstable", "path-unstable", "ptr-unstable", "serialiser-binary-unstable", "serialiser-text-unstable", "string-pool-unstable", "sudoku-unstable", "unicode-unstable"]
all-addons = ["hashbrown", "image", "large-tuples", "nightly", "omega-tuples-of-doom", "serde", "serde-json"]

aoc-unstable = [
Expand Down Expand Up @@ -144,6 +144,7 @@ num-traits-unstable = [
"int-unstable"
]
path-unstable = []
ptr-unstable = []
rand = [
"dep:rand",
"dep:rand_chacha"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ reminder: **Unstable features are NOT covered by semver!**
- **`minesweeper`** - core logic components for minesweeper games of arbitrary size
- **`num-traits`** - traits for number types and number functionality
- **`path`** - UTF-8 only path manipulation utilities written from scratch
- **`ptr`** - small utility functions for references and pointers
- **`serialiser-binary`** - self describing, stable (once finished) binary serialiser, aiming for small output size by exploiting common patterns in real world data
- **`serialiser-text`** - self describing, stable (once finished) text serialiser, aiming for human readability, and ease of writing
- **`string-pool`** - global immutable string pool and String type
Expand Down
4 changes: 4 additions & 0 deletions scripts/src/bin/gen-features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ fn main() {
"path"
"UTF-8 only path manipulation utilities written from scratch"

#[unstable]
"ptr"
"small utility functions for references and pointers"

"rand"
"random number generator lib, building on top of `rand`"
dependencies: ["rand", "rand-chacha"]
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#![cfg_attr(all(not(any(docsrs, kiwingay)), feature = "nominal"), doc = "- `nominal`")]
#![cfg_attr(all(not(any(docsrs, kiwingay)), feature = "num-traits-unstable"), doc = "- `num-traits` (unstable)")]
#![cfg_attr(all(not(any(docsrs, kiwingay)), feature = "path-unstable"), doc = "- `path` (unstable)")]
#![cfg_attr(all(not(any(docsrs, kiwingay)), feature = "ptr-unstable"), doc = "- `ptr` (unstable)")]
#![cfg_attr(all(not(any(docsrs, kiwingay)), feature = "rand"), doc = "- `rand`")]
#![cfg_attr(all(not(any(docsrs, kiwingay)), feature = "serialiser-binary-unstable"), doc = "- `serialiser-binary` (unstable)")]
#![cfg_attr(all(not(any(docsrs, kiwingay)), feature = "serialiser-text-unstable"), doc = "- `serialiser-text` (unstable)")]
Expand Down Expand Up @@ -93,6 +94,7 @@
not(feature = "nominal"),
not(feature = "num-traits-unstable"),
not(feature = "path-unstable"),
not(feature = "ptr-unstable"),
not(feature = "rand"),
not(feature = "serialiser-binary-unstable"),
not(feature = "serialiser-text-unstable"),
Expand Down Expand Up @@ -232,6 +234,10 @@ pub mod num_traits;
#[cfg_attr(docsrs, doc(cfg(feature = "path-unstable")))]
pub mod path;

#[cfg(feature = "ptr-unstable")]
#[cfg_attr(docsrs, doc(cfg(feature = "ptr-unstable")))]
pub mod ptr;

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
pub mod rand;
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
not(feature = "nominal"),
not(feature = "num-traits-unstable"),
not(feature = "path-unstable"),
not(feature = "ptr-unstable"),
not(feature = "rand"),
not(feature = "serialiser-binary-unstable"),
not(feature = "serialiser-text-unstable"),
Expand Down
67 changes: 29 additions & 38 deletions src/ptr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
pub trait PtrExt {
type Out;
fn ref_to_ptr(&self) -> *const Self::Out;
fn ref_to_ptr_mut(&mut self) -> *mut Self::Out;
}

impl<T> PtrExt for T {
type Out = T;
use std::ops::{ Deref, DerefMut };

#[inline(always)]
fn ref_to_ptr(&self) -> *const T {
self
}
#[inline(always)]
pub fn coerce_ptr<T: ?Sized>(thing: &T) -> *const T {
thing
}

#[inline(always)]
fn ref_to_ptr_mut(&mut self) -> *mut T {
self
}
#[inline(always)]
pub fn coerce_ptr_mut<T: ?Sized>(thing: &mut T) -> *mut T {
thing
}

impl<T> PtrExt for [T] {
type Out = T;
#[inline(always)]
pub fn coerce_slice_ptr<T>(thing: &[T]) -> *const T {
coerce_ptr(thing).cast()
}

#[inline(always)]
fn ref_to_ptr(&self) -> *const T {
self.as_ptr()
}
#[inline(always)]
pub fn coerce_slice_ptr_mut<T>(thing: &mut [T]) -> *mut T {
coerce_ptr_mut(thing).cast()
}

#[inline(always)]
fn ref_to_ptr_mut(&mut self) -> *mut T {
self.as_mut_ptr()
}
#[inline(always)]
pub unsafe fn reborrow<'h, T: ?Sized>(thing: *const T) -> &'h T {
unsafe { &*thing }
}

pub trait PtrSliceExt {
fn slice_to_ptr(&self) -> *const Self;
fn slice_to_ptr_mut(&mut self) -> *mut Self;
#[inline(always)]
pub unsafe fn reborrow_mut<'h, T: ?Sized>(thing: *mut T) -> &'h mut T {
unsafe { &mut *thing }
}

impl<T> PtrSliceExt for [T] {
#[inline(always)]
fn slice_to_ptr(&self) -> *const [T] {
self
}
#[inline(always)]
pub unsafe fn deref_ptr<T: Deref>(thing: *const T) -> *const T::Target {
unsafe { &**thing }
}

#[inline(always)]
fn slice_to_ptr_mut(&mut self) -> *mut [T] {
self
}
#[inline(always)]
pub unsafe fn deref_ptr_mut<T: DerefMut>(thing: *mut T) -> *mut T::Target {
unsafe { &mut **thing }
}

0 comments on commit d6c42a2

Please sign in to comment.