Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Anyone know why there isn't a single type/interface that allows for consumers to supply any of Arc, Rc etc boxed values?

I haven't investigated it deeply, but I was developing something in Rust, and whether something needs to be threadsafe or not is entirely on the consumer's use case... bad separation of concerns for the provider of a generic interface to have to specify the specific type of boxed value. 100% fine if the behavior in this case is to pre-allocate the max possible boxed type memory requirement.

This is the only thing I was really frustrated with in Rust



> Anyone know why there isn't a single type/interface that allows for consumers to supply any of Arc, Rc etc boxed values?

Your generic interface just takes a reference to the value inside the box.


Borrowing the value didn't work for this case, but forget the reasoning at the moment. This is a struct that may or may not be used in threaded code. Code is highly polymorphic and involves a few traits

Using Arc everywhere solves it, but dumb and inefficient for non threaded use cases. Maybe compiler optimizes this though, who knows. Semantically it's wrong though.

Honestly forget the specifics enough at this point to discuss so I'll drop it haha.

Was just curious whether somebody else was tracking this, or there was a known workaround. I think it's something the language will eventually support. I saw other threads on rustlang asking for the same thing, and best I saw was some sort of enum style hack representing the boxed types to emulate it


You can use impl Borrow<T> for that if the choice is static.

If it's dynamic, you can use Cow or the supercow/bos/... crates if you want Arc/Rc to be options as well.


I forget the specifics at the moment, but impl Borrow didn't work for my case, I definitely tried it and expected it to work though.

I'll check the Cow crate.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: