Enum naming convention

Coming from Java I am used to collisions between the name I would like to call an interface and an implementation. In the typical case the solution is to add some additional qualifier to the name of the implementation, but if that's not possible the convention is to suffix 'Impl' onto the end of the implementation name and leave the name of the interface alone.

In contrast in C# the convention is to prefix the interface with "I" and leave the implementation as is.

In Rust it seems that in general this problem comes up less because Traits tend to be more focused then Interfaces in Java or C# but the few times I have seen it the "Impl" suffix seems to be used. This seems reasonable to me.

However I recently encountered a case I had no experience with and was not able to find a convention mentioned online: What if it's not one struct acting as an implementation but several included in an enum?

Specifically I have a trait:

trait Request {
   fn get_request_id() -> u64;
   //...
}

And several structs that implement this trait:

impl Request for WriteRequest {
   //...
}
impl Request for ReadRequest {
   //...
}

Then I want to have an enum of the types of request that can be received:

enum RequestXXX {
   Write(WriteRequest),
   Read(ReadRequest),
   //...
}

I am not sure what to call this enum. If it were just a tag IE: "Write, Read" I might call it RequestType or RequestKind. But given that it actually holds the request itself it's not clear to me.

What would you call this enum?

1 Like

I would call the enum Request, since that seems to make sense. It helps that Rust allows disambiguation when users need to import the trait and type:

use the_trait::Request as _; // trait method is usable
use the_type::Request;

The other question is whether it makes sense to not have the trait (dynamic dispatch), since you already have the enum (static dispatch):

impl Request { // the enum
    fn request_id(&self) -> RequestId {
        match self {
            Self::ReadRequest(read_request) => read_request.request_id(),
            Self::WriteRequest(write_request) => write_request.request_id(),
        }
    }
}

The ReadRequest and WriteRequest types can still have the request_id() methods on the type impl themselves, though I guess you won't be able to pass a &Request (trait object) around.

1 Like