ZST that implements Display

Is there a zero-sized type that implements Display ?

Some context:
I'm using petgraph for working with a graph with no edges, that is represented as a Graph<Foo, ()>. But since () does not implement Display, i can't use methods that want to display the edges (for example exporting to a .dot file)

I resolved my issue by just creating an empty struct and implementing Display for it, but i wondered if something like this existed in the standard library

Many error types do, e.g. ParseBoolError in std::str - Rust

I can interpret this question in two ways. Either you are interested in existing types for comparison to reason about whether a Display implementation on a ZST is a reasonable thing in the first place. Answer: Yes, they exist, as shown above. Or you’re asking whether there’s a pre-existing concrete type you can your for your purposes, in which case… well I haven’t fully understood your context yet anyways, but… no, those types that exist in the standard library probably don’t happen to have exactly the Display implementation you’re looking for.

1 Like

For what it's worth, fmt::Error, RecvError, and SendError<ZST> are all guaranteed to have zero size. LayoutError, TryFromSliceError, BorrowError, BorrowMutError, CharTryFromError, TryFromCharError, TryFromIntError, InvalidHandleError, NullHandleError, StripPrefixError, ParseBoolError, FromUtf16Error, PoisonError<ZST>, and AccessError currently have zero size, but this is not a guarantee, and future versions of the standard library may add fields to them. There are also the unstable types AllocError, FromBytesUntilNulError, and GetManyMutError<N> which currently have zero size.

But all of these have their own fixed error messages, as mentioned. Since const generic parameters currently cannot contain strings, if you want a zero-sized Display type with your own custom message, you must create your own.

1 Like

This seems like the best solution. Why would you want anything else?

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.