I needed to have a type coming for the roc side to be send on the rust side so i wrote a new type that clone the contents of roc_std::RocBox
if the ref count is greater than 1 or else take owner ship of the data. Being rather inexperienced in unsafe rust i would like some feed back on what I've written or if it would work at all... Thank you in advance to anyone taking of their time to decipher my code :) Here is the link to the pastebin: https://pastebin.com/TS5AZapC
Edit: I recon something like this could be added to the standard library which could make the code neater as I tried some tricks to get the storage from the RocBox
without having access to the private functions on it.
Here is how i would explain what my code does:
/// UniqueBox is equivalent to RocBox to the exeption of the
/// cloning mechanism. It doesn't increase the reference count
/// it duplicate the data and create a new UniqueBox.
/// UniqueBox can be created for a RocBox. It will check the
/// reference count, if it is one it will effectively transmute
/// the RocBox to UniqueBox. Otherwise it will perform a DeepCopy of its contents.
/// UniqueBox can also be converted to a RocBox wich is trivial.
If you look at RocStr
. We actually already have quite similar types. I think ConstantRocStr
and SendSafeRocStr
. This should be model-able the same way. And yeah, in general, these types are probably needed for anything with a refcount.
Wait I think i was on the main branch not on nightly this whole time xD
Wait i don't know anymore i can't find the types you're talking about either on the main branch or the nightly branch.
Ok nightly was an old brach nvm
I still can't find any derivative types of RocStr
anywhere on the main branch.
Also, I guess we ended up not adding constant RocStr. Just added this method instead:
https://github.com/roc-lang/roc/blob/9aeaa5ca0801d0b62cfc4b4f59092e118c5a7114/crates/roc_std/src/roc_str.rs#L140
Welp I missed it thx
would a pr be welcome for a similar addition to RocBox ?
Yeah, for sure.
Ok i will work on it this evening
thx for the help ^^
Last updated: Jul 05 2025 at 12:14 UTC