Comments (4)
If I may, I would suggest to leave slice pointer casts out for now, but add support for NonNull
pointers.
I'm doing this in some of my projects, and I'm basically copy-pasting cast_ref
and friends. These would be trivial additions.
from bytemuck.
there is no (fully "blessed") way to get the length of a general slice pointer on stable.
Since Rust 1.75.0, with the stabilization of wrapping_byte_add
it is possible to write a correct polyfill of <*mut [T]>::len
on stable.
rust-lang/rust#71146 (comment)
from bytemuck.
I would not necessarily be opposed to these, but a few notes:
- (I assume their failure conditions and semantics would be mostly the same as
cast_ref
/cast_mut
/cast_slice
/cast_slice_mut
? Otherwise I don't really see the point; if they are meant to always succeed you can justas
-cast them.) - As has been mentioned in other issues/PRs,
bytemuck
already has a lot of functions (#132 (comment)) - If these were added, I would expect the corresponding
try_
functions to also exist. - I would expect the ordering of the words in the function names to match the existing functions, i.e. either
ptr
betweenslice
andmut
(my preference:cast_ptr_mut
/cast_slice_ptr_mut
) orptr
aftermut
(cast_mut_ptr
/cast_slice_mut_ptr
). For the second case, IMO it would read better ifcast_ptr
/cast_slice_ptr
hadconst
where the*mut
versions havemut
. cast_slice_ptr
andcast_slice_ptr_mut
could not (currently) be (fully) implemented on stable AFAIK, since there is no (fully "blessed") way to get the length of a general slice pointer on stable (<*const [T]>::len
is unstable. There are some partial workarounds/polyfills listed on the tracking issue but none are completely general and safe.).- Also, making a raw slice pointer with
std::ptr::slice_from_raw_parts
was only stabilized in Rust 1.42.0 which is abovebytemuck
's MSRV of 1.34.0, so the slice pointer functions would probably have to be behind a feature flag anyway.
- Also, making a raw slice pointer with
- Raw slice pointers are allowed to have element-lengths that would give them a byte-length longer than
usize::MAX
bytes, which would introduce an additional failure case tocast_slice_ptr(_mut)
that doesn't exist for other casts: "Casting this slice pointer from the source to destination type would overflow the element-length of the slice", though this could reasonably be folded intoPodCastError::SizeMismatch
I suppose (PodCastError
is not#[non_exhaustive]
, so a new variant could not be added semver-compatibly), or a new error type could be added similar toCheckedCastError
. Example:
let too_long: *const [u32] = std::ptr::slice_from_raw_parts(std::ptr::null(), usize::MAX);
let what_error_should_this_return: Result<*const [u8], PodCastError> = try_cast_slice_ptr(too_long);
from bytemuck.
Yeah, there's a lot more small design work than it might seem at first.
Personally, for volatile access i have the voladdress
crate, which has always served my needs well enough to not bother with raw pointers in bytemuck
from bytemuck.
Related Issues (20)
- Cannot use derive macros when reexporting bytemuck from library HOT 2
- Crate makes assumptions about bool values HOT 5
- Safe `offset_of` for packed structs using `addr_of`? HOT 9
- `ASSERT_SIZE_MULTIPLE_OF` when casting `&[u8]` to `&[[u8; 16]]` HOT 1
- Crate documentation is misleading, implies `Pod` required for all functions. HOT 3
- the `FooBits` type generated by `#[derive(CheckedBitPattern)]` should implement `NoUninit` if `Self` is `NoUninit` HOT 3
- False positive derive error for `TransparentWrapper` around types like `Box` HOT 1
- Potential rustc bug related to deriving ByteEq HOT 2
- no-uninit trait with interior mutability HOT 6
- Deriving Pod for arbitrary generic types HOT 1
- Why use from_bytes when you can use pod_read_unaligned? HOT 5
- Build error on nightly-2024-02-05 HOT 1
- Bytemuck 1.14.2 does not build on stable HOT 1
- Feature request: Casting owned slices or vec, I.e. Box<[u64]> to Box<[u8]> or Vec<u64> to Vec<u8> HOT 4
- conversion from `&[u8]` to `&[T]`? HOT 4
- Allow deriving `Zeroable` for enums which are `#[repr(u32, i8, etc..)]` HOT 2
- Address safety invariants in documentation HOT 1
- No documentation for the FooBits public type generated by derive(CheckedBitPattern) HOT 1
- Casts and conversions for owned, boxed slices HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bytemuck.