Can I give alias to an Enum?

I have this enum

enum Message {
    NewJob(Job),
    Terminate,
}

It basically resembles Option where T is Jobs, Some is NewJob and Terminate is None. The main thing it doesn't have is all the methods provided by Option. So, can I have like an alias for this enum to Option, so that I don't need to reimplement the required methods of Option?

What about type Message = Option<Job>?

But, still I've to use Some and None as enum values. I wanted to use NewJob(job) instead of Some(job) and Terminate instead of None

Which methods in Option that you want to use ?
Considering impl Deref and / or DerefMut with Target=Option ?
Then adding / overriding your own methods

The desired aliases could be achieved this way:

type Message = Option<Job>;
use Option::Some as NewJob;
use Option::None as Terminate;

Thanks for the answer

Actually for my case, this is simple enough for me. I don't need to override any methods, just use them as is

But I would recommend this more stringent construction:

mod message_module {
    pub struct Message {
        pub job: Option<Job>
    }
    impl Message {
        pub fn new_job(job: Job) -> Self {Self{job: Some(job)}}
        pub fn terminated() -> Self {Self{job: None}}
    }
}

Further strictness can be achieved by making job private and redirecting the needed methods to the inner option. Furthermore you may want to add this:

pub enum JobOption {NewJob(Job), Terminated}

impl Message {
    pub fn job(self) -> JobOption {
        match self.job {
            Some(job) => JobOption::NewJob(job),
            None => JobOption::Terminated
        }
    }
}

Depending on which level of rigor is desired.

1 Like

Just out of curiosity, what methods are you looking for?

for now, the map() and take() method