Stream: platform development

Topic: Send wrapper for roc box


view this post on Zulip AdrienDML [he/him] (Mar 20 2023 at 13:37):

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.

view this post on Zulip AdrienDML [he/him] (Mar 20 2023 at 14:27):

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.

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 14:48):

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.

view this post on Zulip AdrienDML [he/him] (Mar 20 2023 at 14:58):

Wait I think i was on the main branch not on nightly this whole time xD

view this post on Zulip AdrienDML [he/him] (Mar 20 2023 at 15:01):

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.

view this post on Zulip AdrienDML [he/him] (Mar 20 2023 at 15:02):

Ok nightly was an old brach nvm

view this post on Zulip AdrienDML [he/him] (Mar 20 2023 at 15:46):

I still can't find any derivative types of RocStr anywhere on the main branch.

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 16:23):

https://github.com/roc-lang/roc/blob/9aeaa5ca0801d0b62cfc4b4f59092e118c5a7114/crates/roc_std/src/roc_str.rs#L667

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 16:23):

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

view this post on Zulip AdrienDML [he/him] (Mar 20 2023 at 16:26):

Welp I missed it thx

view this post on Zulip AdrienDML [he/him] (Mar 20 2023 at 16:28):

would a pr be welcome for a similar addition to RocBox ?

view this post on Zulip Brendan Hansknecht (Mar 20 2023 at 16:28):

Yeah, for sure.

view this post on Zulip AdrienDML [he/him] (Mar 20 2023 at 16:28):

Ok i will work on it this evening

view this post on Zulip AdrienDML [he/him] (Mar 20 2023 at 16:28):

thx for the help ^^


Last updated: Jul 05 2025 at 12:14 UTC