Representing type dependencies

⚓ Rust    📅 2026-01-26    👤 surdeus    👁️ 1      

surdeus

Hi, 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 Send but not Sync and needs a Drop to 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 f32 or 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

Read full topic

🏷️ Rust_feed