Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First-class value support #13

Open
ilammy opened this issue Oct 11, 2019 · 0 comments
Open

First-class value support #13

ilammy opened this issue Oct 11, 2019 · 0 comments

Comments

@ilammy
Copy link
Owner

ilammy commented Oct 11, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant