Representing type dependencies
⚓ Rust 📅 2026-01-26 👤 surdeus 👁️ 1Hi, I am thinking about how to represent a repeating pattern I face with types. The pattern when dealing with ffi / hardware / c lib is often as following:
- when connecting to the device, you get a connection handle
- the connection handle typically is
Sendbut notSyncand needs aDropto close the connection - There are symbols on the device that you can read and write to. To do this, you need to get a symbol handle by using the connection. These also have a
Drop, which needs a connection handle. - The symbol represents builtin types like
f32or structs
Basically you want several types safe Symbol handles, which all need a connection handle.
Lets say Con is the connection type. I have seen these in libraries:
struct SymbolHandle<'con, T> {
con: &'con Con
// marker T
}
or
struct SymbolHandle<T> {
con: Rc<Con>
// marker T
}
or
struct SymbolHandle<T> {
con: Arc<Mutex<Con>>
// marker T
}
I don't really like them, because they are either not Send or need a Mutex. Is there a way to implement this in a library, so that a user of the library can create a type safe "environment" type that is send? Maybe by using just one handle for everything, that one can register the symbols?
Thanks
3 posts - 3 participants
🏷️ Rust_feed