Syntactic sugar/workaround for implementing foreign trait on foreign type?

I'm packing a state representation into a u64, and would like to (re-)implement std::fmt::Debug for u64, but am running into the orphan rules cited in E0117. Neither of the following gets around the first change to the orphan rules linked above:

pub type StateT = u64;
pub use u64 as StateT;

It looks like creating a tuple struct does allow me to implement Debug:

pub struct StateT(u64);

...but doing so of course requires changing many of lines of code where I create and read states and perform bitwise operations, and the resulting code would be more difficult to read, so I'd like to avoid this option.

My use case is a framework for running one of several heuristic search algorithms on one of several instances of one of several problem domains. I'm writing debug output for an algorithm, and I would like to implement Debug for each problem domain's state type.

You can implement the required bitwise operations on StateT by implementing std::ops::BitXor etc. The advantage of using StateT over u64 is that you only implement those operations that make sense for your state. And you can change what you want to behave differently, like Display, Debug, FromStr etc.

2 Likes

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.