如何为具有 "rented" 引用的类型实现特征
How do I implement a trait for a type with "rented" reference
注意:我试图让这个 post 尽可能简洁,完整的代码可以在 https://github.com/pchampin/pair_trait.
找到
问题
我定义了以下特征:
pub trait Pair {
type Item: Borrow<str>;
fn first(&self) -> &Self::Item;
fn second(&self) -> &Self::Item;
}
我为 (T,T)
和 [T;2]
的任何 T 实现了 Borrow<str>
.
的这个特性的通用实现
我还有一个类型,用 rental 板条箱构建,包含一个 String
和两个从该字符串借用的 Cow<str>
:
#[rental(covariant)]
pub struct SelfSustainedPair {
line: String,
pair: (Cow<'line, str>, Cow<'line, str>),
}
我希望这种类型实现上面的 Pair
特性,但我找不到实现它的方法。
尝试 #0
impl SelfSustainedPair {
pub fn first(&self) -> &Cow<str> { self.suffix().first() }
pub fn second(&self) -> &Cow<str> { self.suffix().second() }
}
我知道这不是特性的实现,
但我只是想确保我可以实现方法 first
和 second
。答案是肯定的:上面的代码可以编译。
尝试 #1
impl Pair for SelfSustainedPair {
type Item = Cow<str>;
fn first(&self) -> &Cow<str> { self.suffix().first() }
fn second(&self) -> &Cow<str> { self.suffix().second() }
}
编译失败,第 2 行显示消息 "expected lifetime parameter"。
这令人沮丧,因为它非常接近上面的 #0 尝试。
尝试 #2
impl<'a> Pair for SelfSustainedPair {
type Item = Cow<'a, str>;
fn first(&self) -> &Cow<'a, str> { self.suffix().first() }
fn second(&self) -> &Cow<'a, str> { self.suffix().second() }
}
编译器在这里抱怨第一行 (impl<'a>
) 的 "unconstrainted lifetime parameter"。
尝试#3
我修改了我的特征 Pair
以便它需要一个生命周期参数。令人惊讶的是,即使在特征的定义中从未使用 liferime 参数,这仍然有效...
然后我写道:
impl<'a> Pair<'a> for SelfSustainedPair {
type Item = Cow<'a, str>;
fn first(&self) -> &Cow<'a, str> { self.suffix().first() }
fn second(&self) -> &Cow<'a, str> { self.suffix().second() }
}
现在编译器在两种方法中都抱怨 "cannot infer an appropriate lifetime for autoref"...
无论如何,我的直觉是这不是正确的路径:返回的 Cow
的生命周期不能独立于 self
...
的生命周期指定
尝试 #4
理想情况下,这就是我想写的:
impl Pair for SelfSustainedPair {
type Item = Cow<'self, str>;
fn first(&self) -> &Cow<str> { self.suffix().first() }
fn second(&self) -> &Cow<str> { self.suffix().second() }
}
但显然,编译器不知道 self
生命周期。
不幸的是,目前无法实现完全预期的设计。不过,我们可以将尝试 #3 调整为 类 工作。
returning a &Self::Item
的想法有点不正确,因为关联类型 Item
已经表示借用的值。直接 return 更有意义:
pub trait Pair {
type Item: Borrow<str>;
fn first(&self) -> Self::Item;
fn second(&self) -> Self::Item;
}
但是你会遇到无法将此 Item
描述为 Cow<'a, str>
的情况,其中 'a
是 self
的生命周期。尝试 3 通过向特征本身添加一个生命周期参数来接近解决方案,从而使这个任意生命周期成为特征的更高级别的参数。
pub trait Pair<'a> {
type Item: 'a + Borrow<str>;
fn first(&'a self) -> Self::Item;
fn second(&'a self) -> Self::Item;
}
此 Pair<'a>
现在定义了一对绑定到 'a
的元素,并且不一定包含在 self
中。一种可能的实现方式:
impl<'a> Pair<'a> for (String, String) {
type Item = Cow<'a, str>;
fn first(&'a self) -> Self::Item {
Cow::Borrowed(&self.0)
}
fn second(&'a self) -> Self::Item {
Cow::Borrowed(&self.1)
}
}
Full example in the Rust Playground
这种方法的代价是用更高等级的特征边界污染所有依赖该特征的 API,因此我们可以在所有生命周期 'a
中实现 Pair<'a>
。例如:
fn foo<T>(pair: T)
where
for<'a> T: Pair<'a>,
{
unimplemented!()
}
为了实现约束关联类型 Item
的 'self
生命周期,我们需要 Generic Associated Types (GAT)。一旦实现,我们就可以这样写:
pub trait Pair {
type Item<'a>: Borrow<str>;
fn first(&'a self) -> Self::Item<'a>;
fn second(&'a self) -> Self::Item<'a>;
}
impl Pair for (String, String) {
type Item<'a> = Cow<'a, str>;
fn first(&'a self) -> Self::Item<'a> {
Cow::Borrowed(&self.0)
}
fn second(&'a self) -> Self::Item<'a> {
Cow::Borrowed(&self.1)
}
}
注意:我试图让这个 post 尽可能简洁,完整的代码可以在 https://github.com/pchampin/pair_trait.
找到问题
我定义了以下特征:
pub trait Pair {
type Item: Borrow<str>;
fn first(&self) -> &Self::Item;
fn second(&self) -> &Self::Item;
}
我为 (T,T)
和 [T;2]
的任何 T 实现了 Borrow<str>
.
我还有一个类型,用 rental 板条箱构建,包含一个 String
和两个从该字符串借用的 Cow<str>
:
#[rental(covariant)]
pub struct SelfSustainedPair {
line: String,
pair: (Cow<'line, str>, Cow<'line, str>),
}
我希望这种类型实现上面的 Pair
特性,但我找不到实现它的方法。
尝试 #0
impl SelfSustainedPair {
pub fn first(&self) -> &Cow<str> { self.suffix().first() }
pub fn second(&self) -> &Cow<str> { self.suffix().second() }
}
我知道这不是特性的实现,
但我只是想确保我可以实现方法 first
和 second
。答案是肯定的:上面的代码可以编译。
尝试 #1
impl Pair for SelfSustainedPair {
type Item = Cow<str>;
fn first(&self) -> &Cow<str> { self.suffix().first() }
fn second(&self) -> &Cow<str> { self.suffix().second() }
}
编译失败,第 2 行显示消息 "expected lifetime parameter"。
这令人沮丧,因为它非常接近上面的 #0 尝试。
尝试 #2
impl<'a> Pair for SelfSustainedPair {
type Item = Cow<'a, str>;
fn first(&self) -> &Cow<'a, str> { self.suffix().first() }
fn second(&self) -> &Cow<'a, str> { self.suffix().second() }
}
编译器在这里抱怨第一行 (impl<'a>
) 的 "unconstrainted lifetime parameter"。
尝试#3
我修改了我的特征 Pair
以便它需要一个生命周期参数。令人惊讶的是,即使在特征的定义中从未使用 liferime 参数,这仍然有效...
然后我写道:
impl<'a> Pair<'a> for SelfSustainedPair {
type Item = Cow<'a, str>;
fn first(&self) -> &Cow<'a, str> { self.suffix().first() }
fn second(&self) -> &Cow<'a, str> { self.suffix().second() }
}
现在编译器在两种方法中都抱怨 "cannot infer an appropriate lifetime for autoref"...
无论如何,我的直觉是这不是正确的路径:返回的 Cow
的生命周期不能独立于 self
...
尝试 #4
理想情况下,这就是我想写的:
impl Pair for SelfSustainedPair {
type Item = Cow<'self, str>;
fn first(&self) -> &Cow<str> { self.suffix().first() }
fn second(&self) -> &Cow<str> { self.suffix().second() }
}
但显然,编译器不知道 self
生命周期。
不幸的是,目前无法实现完全预期的设计。不过,我们可以将尝试 #3 调整为 类 工作。
returning a &Self::Item
的想法有点不正确,因为关联类型 Item
已经表示借用的值。直接 return 更有意义:
pub trait Pair {
type Item: Borrow<str>;
fn first(&self) -> Self::Item;
fn second(&self) -> Self::Item;
}
但是你会遇到无法将此 Item
描述为 Cow<'a, str>
的情况,其中 'a
是 self
的生命周期。尝试 3 通过向特征本身添加一个生命周期参数来接近解决方案,从而使这个任意生命周期成为特征的更高级别的参数。
pub trait Pair<'a> {
type Item: 'a + Borrow<str>;
fn first(&'a self) -> Self::Item;
fn second(&'a self) -> Self::Item;
}
此 Pair<'a>
现在定义了一对绑定到 'a
的元素,并且不一定包含在 self
中。一种可能的实现方式:
impl<'a> Pair<'a> for (String, String) {
type Item = Cow<'a, str>;
fn first(&'a self) -> Self::Item {
Cow::Borrowed(&self.0)
}
fn second(&'a self) -> Self::Item {
Cow::Borrowed(&self.1)
}
}
Full example in the Rust Playground
这种方法的代价是用更高等级的特征边界污染所有依赖该特征的 API,因此我们可以在所有生命周期 'a
中实现 Pair<'a>
。例如:
fn foo<T>(pair: T)
where
for<'a> T: Pair<'a>,
{
unimplemented!()
}
为了实现约束关联类型 Item
的 'self
生命周期,我们需要 Generic Associated Types (GAT)。一旦实现,我们就可以这样写:
pub trait Pair {
type Item<'a>: Borrow<str>;
fn first(&'a self) -> Self::Item<'a>;
fn second(&'a self) -> Self::Item<'a>;
}
impl Pair for (String, String) {
type Item<'a> = Cow<'a, str>;
fn first(&'a self) -> Self::Item<'a> {
Cow::Borrowed(&self.0)
}
fn second(&'a self) -> Self::Item<'a> {
Cow::Borrowed(&self.1)
}
}