简体   繁体   中英

How can I implement a marker trait on a struct with a lifetime?

Given a struct that has a lifetime, how can I provide a marker-trait for it.

struct UserProvidedID<'a> { field: &'a str, }
impl warp::reject::Reject for UserProvidedID<'_> {}

Note this trait otherwise does what I want, I just want to "mark" it. I don't want to change the lifetime semantics.

The definition of Reject is the following:

pub trait Reject: Debug + Sized + Send + Sync + 'static { }

Notice the 'static , which means any type you want to implement Reject for needs to accept the 'static lifetime. Therefore, the only possibility is to use the static lifetime in your struct too :

struct UserProvidedID<'a> { field: &'a str, }
impl Reject for UserProvidedID<'static> {}

This might not be what you want, but given the definition of Reject , in a crate I assume is not yours, this is your only option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM