Is anyvec crate of relevance?
Rust Lang
Rules [Developing]
Observe our code of conduct
- Strive to treat others with respect, patience, kindness, and empathy.
- We observe the Rust Project Code of Conduct.
- Submissions must be on-topic
- Posts must reference Rust or relate to things using Rust. For content that does not, use a text post to explain its relevance.
- Post titles should include useful context.
- For Rust questions, use the stickied Q&A thread. [TBD]
- Arts-and-crafts posts are permitted on weekends.
- No meta posts; message the mods instead.
Constructive criticism only
- Criticism is encouraged, though it must be constructive, useful and actionable.
- If criticizing a project on GitHub, you may not link directly to the project’s issue tracker. Please create a read-only mirror and link that instead.
- Keep things in perspective
- A programming language is rarely worth getting worked up over.
- No zealotry or fanaticism.
- Be charitable in intent. Err on the side of giving others the benefit of the doubt.
No endless relitigation
- Avoid re-treading topics that have been long-settled or utterly exhausted.
- Avoid bikeshedding.
- This is not an official Rust forum, and cannot fulfill feature requests. Use the official venues for that.
No low-effort content
- Showing off your new projects is fine
No memes or image macros
- Please find other communities to post memes
No NSFW Content
- There are many other NSFW communities, let’s keep this related to the language
Thanks for the reply. Unfortunately, I could just use Any
for that since Vec
is 'static
as long as the items are (and I don't think you could use anyvec
if the tiems weren't static).
I asked in the Rust Discord channels and it seems like my approach is fine. Even the mutable version is okay and doesn't need to be unsafe if it returns the reference with self
's lifetime.
I asked in the Rust Discord channels and it seems like my approach is fine. Even the mutable version is okay and doesn’t need to be unsafe if it returns the reference with self’s lifetime.
Then maybe publish it as a mini-crate (e.g. any-slice
), ideally with a link to the discussion that proves that it is sound.
The more I think about it, the more I'm convinced the immutable slice version is safe. I'm pretty sure the mutable version in the playground is also safe because you can't turn it into a mutable reference without consuming it and it requires a mutable reference to build.
The mutable version is pretty inconvenient to actually use if you want to store the AnySliceMut
and pass it to other functions. The other function would have to reconstruct it and pass it back once it was done with the mutable reference. But what if:
// ^ impl AnySliceMut
pub unsafe fn as_slice_mut(&mut self) -> Option<&'a mut [T]> {
if TypeId::of::() != self.tid {
return None;
}
Some(unsafe { std::slice::from_raw_parts_mut(self.ptr as *mut T, self.len) })
}
Obviously it's possible to abuse but if I just do something like pass it to a function that takes a mut ref and that function doesn't do anything weird like save the reference is this okay? MIRI is apparently okay with it: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=149ad441a1c66b3f1fd7f2107acbeccf
Note: both code fragments and links contain things like &
which makes them inoperable as is.
Note: both code fragments and links contain things like &
Yes, I know. lemmy mangles stuff like ampersand and less than even inside code blocks. I noted this in an edit for the main post and included a playground link as well.
Playground link is mangled as well: I needed to manually replace &
s with &
to make it load the gist.
Look at your post and you'll see my issue. :)