简体   繁体   English

如何在具有生命周期的结构上实现标记特征?

[英]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: Reject的定义如下:

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.注意'static ,这意味着您想要实现Reject的任何类型都需要接受'static生命周期。 Therefore, the only possibility is to use the static lifetime in your struct too :因此,唯一的可能性是在你的结构中使用 static 生命周期

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.这可能不是您想要的,但鉴于Reject的定义,在我认为不是您的箱子中,这是您唯一的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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