[英]Getting "cannot extract an associated type from a higher-ranked trait bound in this context" error in Rust
[英]How do I return an associated type from a higher-ranked trait bound trait?
我有一个特征,它具有反序列化关联类型的功能。 然而,关联类型需要有调用者决定的生命周期,所以我有一个单独的特征,我使用更高等级的特征绑定,以便它可以在任何生命周期反序列化。
我需要使用一个返回此关联类型的闭包。
我有以下代码来做到这一点:
#![allow(unreachable_code)]
use std::marker::PhantomData;
trait Endpoint: for<'a> EndpointBody<'a> {}
trait EndpointBody<'a> {
type Out: 'a;
fn serialize(body: &Self::Out) -> Vec<u8>;
fn deserialize(raw_body: &'a [u8]) -> Self::Out;
}
// /////////////////////////////////////////////////////////
/// Trait object compatible handler
trait Handler {
fn execute(&self, raw_body: &[u8]) -> Vec<u8>;
}
/// Wraps a function for an endpoint, convertint it to a Handler
struct FnHandler<EP, F>
where
EP: Endpoint,
F: 'static + for<'a> Fn(&'a [u8]) -> <EP as EndpointBody<'a>>::Out,
{
func: F,
_ph: PhantomData<EP>,
}
impl<EP, F> FnHandler<EP, F>
where
EP: Endpoint,
F: 'static + for<'a> Fn(&'a [u8]) -> <EP as EndpointBody<'a>>::Out,
{
pub fn new(func: F) -> Self {
Self {
func,
_ph: PhantomData,
}
}
}
impl<EP, F> Handler for FnHandler<EP, F>
where
EP: Endpoint,
F: 'static + for<'a> Fn(&'a [u8]) -> <EP as EndpointBody<'a>>::Out,
{
fn execute(&self, in_raw_body: &[u8]) -> Vec<u8> {
let body = (self.func)(in_raw_body);
let serialized_body = unimplemented!();
return serialized_body;
}
}
// /////////////////////////////////////////////////////////
/// Collection of handlers
struct Handlers(Vec<Box<dyn Handler>>);
impl Handlers {
pub fn new() -> Self {
Self(vec![])
}
pub fn handle<EP: 'static, F>(&mut self, func: F)
where
EP: Endpoint,
F: 'static + for<'a> Fn(&'a [u8]) -> <EP as EndpointBody<'a>>::Out,
{
self.0.push(Box::new(FnHandler::<EP, F>::new(func)));
}
}
// /////////////////////////////////////////////////////////
struct MyEndpoint;
struct MyEndpointBody<'a> {
pub string: &'a str,
}
impl Endpoint for MyEndpoint {}
impl<'a> EndpointBody<'a> for MyEndpoint {
type Out = MyEndpointBody<'a>;
fn serialize(body: &Self::Out) -> Vec<u8> {
unimplemented!()
}
fn deserialize(raw_body: &'a [u8]) -> Self::Out {
unimplemented!()
}
}
// /////////////////////////////////////////////////////////
fn main() {
let mut handlers = Handlers::new();
handlers.handle::<MyEndpoint, _>(|_body| MyEndpointBody {
string: "test string",
});
handlers.0[1].execute(&[]);
}
我认为这应该可行,但是当我检查它时,我收到一个类型错误:
error[E0271]: type mismatch resolving `for<'a> <[closure@src/main.rs:92:38: 94:6] as std::ops::FnOnce<(&'a [u8],)>>::Output == <MyEndpoint as EndpointBody<'a>>::Out`
--> src/main.rs:92:14
|
92 | handlers.handle::<MyEndpoint, _>(|_body| MyEndpointBody {
| ^^^^^^ expected struct `MyEndpointBody`, found associated type
|
= note: expected struct `MyEndpointBody<'_>`
found associated type `<MyEndpoint as EndpointBody<'_>>::Out`
= note: consider constraining the associated type `<MyEndpoint as EndpointBody<'_>>::Out` to `MyEndpointBody<'_>`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
这很令人困惑,因为MyEndpoint::Out
是一个MyEndpointBody
,我从闭包中返回它,但 Rust 不认为它们是相同的类型。 我猜这是因为 Rust 为MyEndpointBody
类型选择了不兼容的匿名生命周期,但我不知道如何解决这个问题。
我怎样才能让这段代码工作,以便我可以使用一个带有 HRTB 关联类型的闭包?
让闭包将返回类型包装在 newtype 中解决了这个问题:
#![allow(unreachable_code)]
use std::marker::PhantomData;
trait Endpoint: for<'a> EndpointBody<'a> {}
trait EndpointBody<'a> {
type Out: 'a;
fn serialize(body: &Self::Out) -> Vec<u8>;
fn deserialize(raw_body: &'a [u8]) -> Self::Out;
}
struct EPOut<'a, EP: Endpoint>(<EP as EndpointBody<'a>>::Out);
// /////////////////////////////////////////////////////////
/// Trait object compatible handler
trait Handler {
fn execute(&self, raw_body: &[u8]) -> Vec<u8>;
}
/// Wraps a function for an endpoint, convertint it to a Handler
struct FnHandler<EP, F>
where
EP: Endpoint,
F: 'static + for<'a> Fn(&'a [u8]) -> EPOut<'a, EP>,
{
func: F,
_ph: PhantomData<EP>,
}
impl<EP, F> FnHandler<EP, F>
where
EP: Endpoint,
F: 'static + for<'a> Fn(&'a [u8]) -> EPOut<'a, EP>,
{
pub fn new(func: F) -> Self {
Self {
func,
_ph: PhantomData,
}
}
}
impl<EP, F> Handler for FnHandler<EP, F>
where
EP: Endpoint,
F: 'static + for<'a> Fn(&'a [u8]) -> EPOut<'a, EP>,
{
fn execute(&self, in_raw_body: &[u8]) -> Vec<u8> {
let body = (self.func)(in_raw_body);
let serialized_body = unimplemented!();
return serialized_body;
}
}
// /////////////////////////////////////////////////////////
/// Collection of handlers
struct Handlers(Vec<Box<dyn Handler>>);
impl Handlers {
pub fn new() -> Self {
Self(vec![])
}
pub fn handle<EP: 'static, F>(&mut self, func: F)
where
EP: Endpoint,
F: 'static + for<'a> Fn(&'a [u8]) -> EPOut<'a, EP>,
{
self.0.push(Box::new(FnHandler::<EP, F>::new(func)));
}
}
// /////////////////////////////////////////////////////////
struct MyEndpoint;
struct MyEndpointBody<'a> {
pub string: &'a str,
}
impl Endpoint for MyEndpoint {}
impl<'a> EndpointBody<'a> for MyEndpoint {
type Out = MyEndpointBody<'a>;
fn serialize(body: &Self::Out) -> Vec<u8> {
unimplemented!()
}
fn deserialize(raw_body: &'a [u8]) -> Self::Out {
unimplemented!()
}
}
// /////////////////////////////////////////////////////////
fn main() {
let mut handlers = Handlers::new();
handlers.handle::<MyEndpoint, _>(|_body| EPOut(MyEndpointBody {
string: "test string",
}));
handlers.0[1].execute(&[]);
}
我很想说这是一个 Rust 编译器错误,因为 newtype 应该与关联类型几乎相同。 似乎还有一些与使用 HRTB 相关类型相关的 ICE: https : //github.com/rust-lang/rust/issues/62529
能否请你检查这一个
trait Endpoint: for<'a> DeserializeBody<'a> {}
trait DeserializeBody<'a> {
type Out: 'a;
fn deserialize(raw_body: &'a [u8]) -> Self::Out;
}
fn store_ep<'a, EP, F>(func: F)
where
EP: Endpoint,
F: 'static + Fn(&'a [u8]) -> <EP as DeserializeBody<'a>>::Out,
{
let _ = Box::new(func);
unimplemented!();
}
// /////////////////////////////////////////////////////////
struct MyEndpoint;
struct MyEndpointBody<'a> {
pub string: &'a str,
}
impl Endpoint for MyEndpoint {}
impl<'a> DeserializeBody<'a> for MyEndpoint {
type Out = MyEndpointBody<'a>;
fn deserialize(raw_body: &'a [u8]) -> Self::Out {
unimplemented!();
}
}
// /////////////////////////////////////////////////////////
fn main() {
store_ep::<MyEndpoint, _>(|raw_body| MyEndpointBody { string: "test" });
}
将DeserializeBody
定义为:
trait DeserializeBody {
type Out;
fn deserialize(raw_body: &[u8]) -> Self::Out;
}
Out
是泛型类型的声明。 不要在这里声明生命周期绑定,它会在定义站点上明确。
在这一点上,不再需要为Endpoint
更高级的 Trait Bound :
trait Endpoint: DeserializeBody {}
trait DeserializeBody {
type Out;
fn deserialize(raw_body: &[u8]) -> Self::Out;
}
在定义站点,必须为关联的类型Out
表达生命周期要求。 如果DeserializeBody
不再是通用的,那么MyEndpoint
必须是:
impl<'a> DeserializeBody for MyEndpoint<'a> {
type Out = MyEndpointBody<'a>;
...
为了实现这样的要求,让我们求助于需要生命周期'a
的幻影类型。
把所有的部分放在一起:
use core::marker::PhantomData;
trait Endpoint: DeserializeBody {}
trait DeserializeBody {
type Out;
fn deserialize(raw_body: &[u8]) -> Self::Out;
}
fn store_ep<EP, F>(func: F)
where
EP: Endpoint,
F: 'static + for<'a> Fn(&'a [u8]) -> <EP as DeserializeBody>::Out,
{
let _ = Box::new(func);
unimplemented!();
}
struct MyEndpoint<'a> {
phantom: PhantomData<&'a ()>
}
struct MyEndpointBody<'a> {
pub string: &'a str,
}
impl<'a> Endpoint for MyEndpoint<'a> {}
impl<'a> DeserializeBody for MyEndpoint<'a> {
type Out = MyEndpointBody<'a>;
fn deserialize(raw_body: &[u8]) -> Self::Out {
unimplemented!();
}
}
fn main() {
store_ep::<MyEndpoint, _>(|raw_body| MyEndpointBody { string: "test" });
}
我认为问题在于您要求您的处理程序能够处理具有该 HK 约束的所有可能的生命周期 - 编译器无法证明这一点已得到验证,因此无法使等价MyEndpointBody <=> MyEndpoint::Out
。
相反,如果您将处理程序参数化为具有单个生命周期,则它似乎按要求编译( playground link ):
#![allow(unreachable_code)]
use std::marker::PhantomData;
trait Endpoint: for<'a> EndpointBody<'a> {}
trait EndpointBody<'a> {
type Out: 'a;
fn serialize(body: &Self::Out) -> Vec<u8>;
fn deserialize(raw_body: &'a [u8]) -> Self::Out;
}
/// Trait object compatible handler
trait Handler<'a> {
fn execute(&self, raw_body: &'a [u8]) -> Vec<u8>;
}
/// Wraps a function for an endpoint, convertint it to a Handler
struct FnHandler<EP, F>
where
EP: Endpoint,
F: 'static,
{
func: F,
_ph: PhantomData<EP>,
}
impl<EP, F> FnHandler<EP, F>
where
EP: Endpoint,
F: 'static,
{
pub fn new(func: F) -> Self {
Self {
func,
_ph: PhantomData,
}
}
}
impl<'a, EP, F> Handler<'a> for FnHandler<EP, F>
where
EP: Endpoint,
F: 'static + Fn(&'a [u8]) -> <EP as EndpointBody<'a>>::Out,
{
fn execute(&self, in_raw_body: &'a [u8]) -> Vec<u8> {
let body = (self.func)(in_raw_body);
let serialized_body = unimplemented!();
return serialized_body;
}
}
// /////////////////////////////////////////////////////////
/// Collection of handlers
struct Handlers<'a>(Vec<Box<dyn Handler<'a>>>);
impl<'a> Handlers<'a> {
pub fn new() -> Self {
Self(vec![])
}
pub fn handle<EP: 'static, F>(&mut self, func: F)
where
EP: Endpoint,
F: 'static + Fn(&'a [u8]) -> <EP as EndpointBody<'a>>::Out,
{
self.0.push(Box::new(FnHandler::<EP, F>::new(func)));
}
}
// /////////////////////////////////////////////////////////
struct MyEndpoint;
struct MyEndpointBody<'a> {
pub string: &'a str,
}
impl Endpoint for MyEndpoint {}
impl<'a> EndpointBody<'a> for MyEndpoint {
type Out = MyEndpointBody<'a>;
fn serialize(body: &Self::Out) -> Vec<u8> {
unimplemented!()
}
fn deserialize(raw_body: &'a [u8]) -> Self::Out {
unimplemented!()
}
}
// /////////////////////////////////////////////////////////
fn main() {
let mut handlers = Handlers::new();
handlers.handle::<MyEndpoint, _>(|_body| MyEndpointBody {
string: "test string",
});
handlers.0[1].execute(&[]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.