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

Use RefCell to prevent multiple mutable references of current value #3

Closed
bvssvni opened this issue Nov 2, 2014 · 8 comments

Comments

@bvssvni
Copy link
Member

commented Nov 2, 2014

As suggested on reddit, this enforces the safety at runtime and might be combined with the design such that the current value can only be borrowed once as mutable but multiple times as immutable.

@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 2, 2014

Reddit comment: #3

@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 2, 2014

If we put an extra field in the hash table, then we can tell whether it is borrowed mutable or immutable.

@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 2, 2014

RefCell uses an uint with -1 as WRITING and 0 as UNUSED. The other values are used for immutable references.

http://doc.rust-lang.org/src/core/home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libcore/cell.rs.html#223-228

@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 2, 2014

Perhaps best to use similar conventions as the standard library.

  • Current::try_borrow
  • Current::borrow
  • Current::try_borrow_mut
  • Current::borrow_mut
@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 2, 2014

Suggestion for names: RefCurrent and RefMutCurrent

  • Implement Drop for RefCurrent
  • Implement Deref for RefCurrent
  • Implement Show for RefCurrent
  • Implement Drop for RefMutCurrent
  • Implement Deref for RefMutCurrent
  • Implement Show for RefMutCurrent
@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 2, 2014

In order to avoid having to type "let foo: RefMutCurrent = Current::borrow_mut(scope)" we could do "let foo = Current::borrow_mut::::(scope);".

@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 2, 2014

Alternative is to use immutable references and use RefCell as convention for mutable ones.

@bvssvni bvssvni added the discussion label Nov 2, 2014

@bvssvni

This comment has been minimized.

Copy link
Member Author

commented Nov 2, 2014

This will not be implemented in favor of using RefCell with immutable references.

@bvssvni bvssvni closed this Nov 2, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.