r/rust • • 2d ago

🙋 seeking help & advice Is this bad practice?

I have some type In a Rc<RefCell<T>> that I need to pass around to some objects to use it mutable. But I also want to pass it to be stored and read by some other object so I did this:

struct ReadRef<T> {
inner: Rc<RefCell<T>>,
}
impl<T> ReadRef<T> {
fn new(inner: Rc<RefCell<T>>) -> Self {
Self { inner }
}
fn borrow(&self) -> Ref<'_, T>
self.inner.borrow(
}
}

2 Upvotes

20 comments sorted by

16

u/NotDuckie 2d ago

Depends entirely on what you are actually trying to do

1

u/Koltaia30 2d ago

This is an object that stores some central information that changes and a lot of objects need it. I am interested if it is justified this patter is okay or if there is some strictly better solution

16

u/NotDuckie 2d ago

What kind of central information? Global mutating state like this is often a code smell.

1

u/Koltaia30 2d ago

Configuration of the software

14

u/NotDuckie 2d ago

Still tells me nothing. What kind of config? What data is stored? When and why is it mutated? How often? Only one potential place at a time? What kind of app?

This is a very nuanced question, so you can't really get a good answer with this little context

1

u/Koltaia30 2d ago

It's for a game and it's configuration for a lot of things and the scene creator should have access to all config and setup stuff with it but should not mutate it. The config is made up of observable variables

6

u/Zde-G 2d ago

Then you are creating a landmine for yourself, because that would mean that some parts of game would work with old config and some with new.

That's why you got no answer. The full answer is, more-or-less: yes, it's usually a bad practice, however something it's also a necessary one… and you need to know more to say if it's needed or not.

1

u/UltramanQuar 2d ago

separate game logic. create game mode and game state, you can make them static

6

u/Lucretiel Datadog 2d ago

This is an object that stores some central information that changes and a lot of objects need it.

To answer your original question: this is bad practice.

0

u/Koltaia30 2d ago

Also it feels so obvious that I am wondering if there is a good reason something like this is not part of the standard library already

5

u/Zde-G 2d ago

It's not part of standard library precisely because “it's so obvious”… and wrong. Most of the time.

Remember? There is always a well-known solution to every human problem — neat, plausible, and wrong… this one example.

8

u/dwalker109 2d ago

If it is created once on app init then just passing a reference around is the simplest option.

A static is also a reasonable option, via something like LazyLock.

1

u/Surfernick1 2d ago

if you initialize it and never mutate again, initialize it on the main thread and then leak via a Box into a &'static

https://archive.is/RFtRS

6

u/afdbcreid 2d ago

It's almost a bad practice to use Rc<RefCell<T>>.

If you use it where you should, then "hiding" like that (which does not really hide anything) is a bad practice, yes. For configuration, for example (like you said in the comments) put individual methods to change each field, and it might be better to wrap each field in a Cell or RefCell individually.

2

u/teerre 2d ago

Rc refcell is most likely a bad practice. Not necessarily, but likely. The problem you ask seen to just be a consequence of a bad design

2

u/sogepunk1 2d ago

Would have to see the rest of the code. Wouldn't say by itself it's a bad practice. You're just wrapping the shared data in a way that only allows for reading.

2

u/phazer99 2d ago

If you're only going to read it, there's no need to use RefCell. First you can mutate it with ownership and after that put it into an Rc for shared reading.

0

u/Sea_Mud6698 2d ago

It is probably fine. You may want to hide the implementation detail that it is even an rc and have specific methods to do the borrows behind the scenes. You will also probably need a weak equivalent.