如何在具有生命周期的结构上实现标记特征?
How can I implement a marker trait on a struct with a lifetime?
给定一个有生命周期的结构,我如何为它提供标记特征。
struct UserProvidedID<'a> { field: &'a str, }
impl warp::reject::Reject for UserProvidedID<'_> {}
注意这个特征否则会做我想要的,我只是想“标记”它。我不想更改生命周期语义。
pub trait Reject: Debug + Sized + Send + Sync + 'static { }
注意 'static
,这意味着您要实现 Reject
的任何类型都需要接受 'static
生命周期。所以,唯一的可能就是use the static lifetime in your struct too:
struct UserProvidedID<'a> { field: &'a str, }
impl Reject for UserProvidedID<'static> {}
这可能不是您想要的,但考虑到 Reject
的定义,在我认为不是您的板条箱中,这是您唯一的选择。
给定一个有生命周期的结构,我如何为它提供标记特征。
struct UserProvidedID<'a> { field: &'a str, }
impl warp::reject::Reject for UserProvidedID<'_> {}
注意这个特征否则会做我想要的,我只是想“标记”它。我不想更改生命周期语义。
pub trait Reject: Debug + Sized + Send + Sync + 'static { }
注意 'static
,这意味着您要实现 Reject
的任何类型都需要接受 'static
生命周期。所以,唯一的可能就是use the static lifetime in your struct too:
struct UserProvidedID<'a> { field: &'a str, }
impl Reject for UserProvidedID<'static> {}
这可能不是您想要的,但考虑到 Reject
的定义,在我认为不是您的板条箱中,这是您唯一的选择。