TypeId<T> guarantees

Is TypeId guaranteed to be identical on separate compilations, but on the same rustc version?

I doubt it; it’s just a u64 internally. I suspect there’s a counter somewhere that gets bumped every time a new type is monomorphized, which means minor changes in the code being compiled could change it.

Probably not. I would absolutely not rely on it for correctness of your program.

type_id is a hash:

It is a known issue that this could cause hash collisions:

https://github.com/rust-lang/rust/issues/10389

2 Likes

Alright. Is there a way I can work with raw pointers maybe and not violate Rust's rules?

Rust objects don’t generally carry their type information with them in memory; if they did, it would almost certainly be a copy of the TypeId, with its drawbacks. What are you actually trying to do?

I'm writing a nodetree execution system, where users could write their own nodes (basically functions) that are defined and created at runtime. I really just need a *void analog in Rust that I can cast to *void and then back down to the type (while also being destructible at the end).

Is the Any type what you're looking for?

Givens that op is looking for something that's stable across compiler versions, it seems they're trying to serialize this type information. Unfortunately, it isn't, and the Any trait depends on this, so a solution which uses its TypeId directly wouldn't work.

Perhaps if you annotate all structures you want to use by implementing a trait with the name of it:

trait Named {
    const UUID: Uuid = Uuid { /* */ };
}

Since UUIDs are consistent as long as you don't change them, you can use them to create a kind of lookup table using a hashmap to potentially correct a changed internal implementation of TypeId.

While not the most elegant solution, this is just an idea I thought to put out there.

It doesn't need to be stable across compiler versions, that will be fixed. I do need it to be stable across different compilations though, and I'd really really not want to do the trait approach.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.