Fastest way to check if a WeakRc points at a real object?

I have a Weak<T>. I want to know if it points at a real object or a null. (i.e. get a bool).

The current options seem to be:

  1. strong_count ( https://doc.rust-lang.org/src/alloc/rc.rs.html#1311-1317 ) > 0

  2. upgrade ( https://doc.rust-lang.org/src/alloc/rc.rs.html#1295-1303 ) != None

As it turns out, need neither a ptr nor a count. I just want to know if it’s null or not. What is the cheapest way to do this (and what is the cost of the cheapest way).

There won’t be anything faster than strong_count > 0, since that is what any method would have to do under the hood. I expect the upgrade option would also optimize to this, but haven’t done any tests.

1 Like