Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upBest coding practices with current objects #15
Comments
bvssvni
added
the
information
label
Nov 4, 2014
bvssvni
closed this
Nov 5, 2014
bvssvni
referenced this issue
Nov 8, 2014
Closed
`Get` and `Set` workaround with multiple constraints #26
bvssvni
referenced this issue
Apr 2, 2015
Closed
Mutable aliased references must be handled carefully in unsafe code #866
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bvssvni commentedNov 4, 2014
Notice! piston-current is an experimental library and should be used with caution!
This library is intended to for two different use cases:
Piston-Current is convenient for game prototyping because it is easy to refactor between references and current objects, thereby saving the amount of rewriting of function signatures, but in such cases caution is required and one is expected to follow the safety guidelines.
High level libraries is still at an early phase of experimenting and it not recommended as a library design pattern yet.
Safety guidelines
Unsafe version (for game prototyping only):
Safe version (this is recommended):
When you want to use the current object in a function, you do this:
For the safe version, use
Rc<RefCell<T>>instead ofT. Example project start_piston.Inside the function where you use the current object, you can call the function and use it as an object:
Unsafe version:
For the unsafe version only keep one mutable pointer in scope. Failing to do so can lead to undefined behavior.
Safe version:
Unsafe use of dereference
Dereferencing, borrowing and then assigning with unsafe current objects multiple times in same scope is dangerous. For example, the following prints out "bar":
It is also dangerous to call functions with references to current objects that uses the same current objects.
This can lead to unexpected behavior under target specific compiler optimization and might not be caught by testing. Therefore, always use the safe version in libraries.