Comments (7)
Proposal
- Have
foreign_obj_type
forcefully implementSend
+Sync
- Mark all the methods (non-static, corresponding to ObjC messages) with
&mut self
- Mark atomic property get/set accessors with
&self
- Mark non-atomic properties with
&mut self
.
Clarification
Atomicity of properties in ObjC guarantees no visible partial writes. While Apple docs say this is not thread safe, they mean data-races in fact, and it's perfectly thread-safe in Rust terms (of memory safety).
from metal-rs.
@gfx-rs/metallists
Comments? Questions? Objections?
This is an important thing for us to decide.
from metal-rs.
Also pinging @SSheldon, the author of objc
bindings in Rust
from metal-rs.
Sorry, I realized why this isn't going to work 😞 . &mut self
means nothing if we are able to have multiple independent rust objects corresponding to the same ObjC object. There has to be something our Rust objects share between each other, e.g. all being Arc<RwLock<ObjcClassRef>>
but with Arc
replaced by something that uses native ObjC
runtime counters.
from metal-rs.
Current approach
A bit of context here, that's what we currently have for ObjC bindings of Metal:
- a
SomeClassRef
type with actual methods (immutable!) for properties and ObjC messages. It's a type-system thing in Rust that doesn't affect any ObjC reference counts. Type system guarantees that the object is alive for the lifetime of our XXXRef struct. - a
SomeClass
type that represent a strong reference to ObjC object. Cloning it is akin cloning anArc
. One can borrow it and getSomeClassRef
temporarily.
Analysis
The main problem for getting any sort of type assistance here is that SomeClass
objects don't share anything with each other, so we don't know (even at run-time) if those point to the same underlying ObjC object...
So here comes the main idea and restriction: let's enforce a guarantee that there is a single Rust object backing a single ObjC object. On Rust side, there is no point to mess with ObjC reference counters. Instead, we can use the regular Arc
and locking primitives. As long as ObjC runtime is concerned, we only have one object.
Unresolved Problems
Any methods that return new objects to use (newXXX
) would just return SomeClass
by value, and the client code would then be responsible for wrapping it into sharing structures, if needed. The destructor of SomeClass
would then release the only ObjC count.
What's not clear, however, is what should be done about getters that return existing objects. e.g.
let obj1 = msg_send![parent.get_some_object];
let obj2 = msg_send![parent.get_some_object];
from metal-rs.
An important missing piece of this puzzle is to how we can uniquely track ObjC objects, and @jrmuizel suggested to put the locking internals into the associated data for fast lookups (by our wrappers around ObjC objects, e.g. struct Device {...}
).
from metal-rs.
Having objects be uniquely owned by a SomeClass
in rust would allow you to take the most advantage of rust's type system; I tried something a little like this with UIKit, but really struggled with ensuring that uniqueness. I haven't worked with Metal, maybe it's better suited? In UIKit, it seems like everything has a reference to everything... 😛
I like the idea of using associated types, that could make it a lot easier to verify that the uniqueness isn't being violated and could make this all much more doable.
from metal-rs.
Related Issues (20)
- Don't use auto-release pool in string-related methods
- Using metal-cpp for auto-generating bindings HOT 2
- Investigate use of objc_retainAutoreleasedReturnValue HOT 6
- Make auto-release pools visible HOT 1
- Different behaviour using Metal with Swift in Xcode vs Rust HOT 2
- Incorrect buffer value in compute example HOT 2
- Export MTLAccelerationStructure
- computation performs slower than cpu version in benchmark HOT 3
- Including `metal-rs` on `cargo.toml` results in a compile error due to `cocoa` HOT 1
- trouble with xcode shader debugger locating source code HOT 1
- Question about mesh shaders HOT 3
- Updating version on crates.io HOT 3
- Can't create indirect command buffers
- `new_render_command_encoder` assumes the return value can never be NULL
- MPS Matrix Multiplication HOT 2
- UB in `new_texture` if `newTextureWithDescriptor` fails? HOT 3
- Kernel stops running when I click on another window. HOT 1
- Inconsistent Performance across runs HOT 4
- Discrepency between GPU timing and CPU timing HOT 3
- Example does not support the latest winit HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from metal-rs.