From d6c42a2733a78ada6c97907a13633c54b57075b7 Mon Sep 17 00:00:00 2001 From: Meadow Liu Date: Sun, 21 Jul 2024 20:55:48 -0700 Subject: [PATCH] uhm, put back ptr fns and something? still, not sure if I like this lol, but it's, something --- Cargo.toml | 3 +- README.md | 1 + scripts/src/bin/gen-features.rs | 4 ++ src/lib.rs | 6 +++ src/prelude.rs | 1 + src/ptr/mod.rs | 67 ++++++++++++++------------------- 6 files changed, 43 insertions(+), 39 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f56bda5e1..9ad30646c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [ @@ -144,6 +144,7 @@ num-traits-unstable = [ "int-unstable" ] path-unstable = [] +ptr-unstable = [] rand = [ "dep:rand", "dep:rand_chacha" diff --git a/README.md b/README.md index 16c789bfd..bc1a31098 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/scripts/src/bin/gen-features.rs b/scripts/src/bin/gen-features.rs index 9d1dfdda4..a1a71b866 100644 --- a/scripts/src/bin/gen-features.rs +++ b/scripts/src/bin/gen-features.rs @@ -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"] diff --git a/src/lib.rs b/src/lib.rs index 3a7fd64bb..dddd3f285 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)")] @@ -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"), @@ -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; diff --git a/src/prelude.rs b/src/prelude.rs index 65203b2f1..ec9658260 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -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"), diff --git a/src/ptr/mod.rs b/src/ptr/mod.rs index 09346e40e..2b2c17cf3 100644 --- a/src/ptr/mod.rs +++ b/src/ptr/mod.rs @@ -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 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(thing: &T) -> *const T { + thing +} - #[inline(always)] - fn ref_to_ptr_mut(&mut self) -> *mut T { - self - } +#[inline(always)] +pub fn coerce_ptr_mut(thing: &mut T) -> *mut T { + thing } -impl PtrExt for [T] { - type Out = T; +#[inline(always)] +pub fn coerce_slice_ptr(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(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 PtrSliceExt for [T] { - #[inline(always)] - fn slice_to_ptr(&self) -> *const [T] { - self - } +#[inline(always)] +pub unsafe fn deref_ptr(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(thing: *mut T) -> *mut T::Target { + unsafe { &mut **thing } }