fluid-let icon indicating copy to clipboard operation
fluid-let copied to clipboard

First-class value support

Open ilammy opened this issue 6 years ago • 0 comments

As I remember, dynamic variables have this weird Option<&T> type and all this UnsafeCell magic inside because I wanted to be able to safely place references inside them.

However, this is kind of an overkill for value types, especially simple ones, like bool. How about putting the actual value inside the variable while still providing scoped get() accessor and scoped assignment guards?

In this way we could get away from using Option and generally simplify the API. The users will still be able to explicitly use Option if they would like it. But they could also use some definite value as the default (e.g., false).

I believe that it won’t be possible to put references to local variables or heap inside as they must ’static:

fluid_let(static MUH_SLICE: Option<&[u8]> = None);

let vec = vec![1, 2, 3];

fluid_set(MUH_SLICE, Some(&vec));

But it seems a safe thing to do so there might we a way.

Also, if you do it in a straightforward manner by moving the old value into the guard, this will result in unnecessary moves when setting and restoring the values. On the other side, if you keep a reference to the local variable then you don’t actually need to move anything, but that’s an additional indirection in getting the value.

ilammy avatar Oct 11 '19 20:10 ilammy