为什么可以box< dyn特征>用& mut特征作为参数启用函数
我敢肯定,以前已经问过,但没有遇到一个问题,可以捕捉这里的精确情况。我有以下代码:
let mut pool: Box<dyn redis::aio::ConnectionLike> = <...>
redis::cmd(COMMAND)
.arg(LIST)
.arg(value)
.query_async(&mut pool)
.await
.unwrap();
这返回错误:
error[E0277]: the trait bound `std::boxed::Box<dyn redis::aio::ConnectionLike>: redis::aio::ConnectionLike` is not satisfied
--> svix-server/src/queue/redis.rs:66:30
|
66 | .query_async(&mut pool)
| ----------- ^^^^^^^^^ the trait `redis::aio::ConnectionLike` is not implemented for `std::boxed::Box<dyn redis::aio::ConnectionLike>`
| |
| required by a bound introduced by this call
|
问题1-为什么错误说该性状未针对box&lt; dyn redis :: aio :: aio :: connection like&gt;
?至少不应该说&amp; box ...
吗?
无论如何,如果我尝试将pool.as_mut()
转换为query_async
,我会收到此错误:
error[E0277]: the size for values of type `dyn redis::aio::ConnectionLike` cannot be known at compilation time
--> svix-server/src/queue/redis.rs:66:30
|
66 | .query_async(pool.as_mut())
| ----------- ^^^^^^^^^^^^^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: the trait `Sized` is not implemented for `dyn redis::aio::ConnectionLike`
问题2-为什么sip sip sized
必需这里? Rust中的参考不是始终大小
吗?
问题3-是否有任何方法可以将dyn Connection
传递给
query_async
方法,例如
#[inline]
#[cfg(feature = "aio")]
pub async fn query_async<C, T: FromRedisValue>(&self, con: &mut C) -> RedisResult<T>
where
C: crate::aio::ConnectionLike,
{
...
}
I'm sure this has been asked previously but haven't encountered a question that captures the precise scenario here. I have the following code:
let mut pool: Box<dyn redis::aio::ConnectionLike> = <...>
redis::cmd(COMMAND)
.arg(LIST)
.arg(value)
.query_async(&mut pool)
.await
.unwrap();
This returns the error:
error[E0277]: the trait bound `std::boxed::Box<dyn redis::aio::ConnectionLike>: redis::aio::ConnectionLike` is not satisfied
--> svix-server/src/queue/redis.rs:66:30
|
66 | .query_async(&mut pool)
| ----------- ^^^^^^^^^ the trait `redis::aio::ConnectionLike` is not implemented for `std::boxed::Box<dyn redis::aio::ConnectionLike>`
| |
| required by a bound introduced by this call
|
Question 1 -- Why does the error say the trait is not implemented for Box<dyn redis::aio::ConnectionLike>
? Shouldn't it at least say &Box...
?
Regardless, if I instead try to pass pool.as_mut()
to query_async
, I get this error:
error[E0277]: the size for values of type `dyn redis::aio::ConnectionLike` cannot be known at compilation time
--> svix-server/src/queue/redis.rs:66:30
|
66 | .query_async(pool.as_mut())
| ----------- ^^^^^^^^^^^^^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: the trait `Sized` is not implemented for `dyn redis::aio::ConnectionLike`
Question 2 -- Why is Sized
required here? Isn't a reference in Rust always Sized
?
Question 3 -- Is there any way to to pass a dyn ConnectionLike
to this
The query_async
method, for reference looks like this:
#[inline]
#[cfg(feature = "aio")]
pub async fn query_async<C, T: FromRedisValue>(&self, con: &mut C) -> RedisResult<T>
where
C: crate::aio::ConnectionLike,
{
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,因为引用不是通用类型
c
的一部分。 Rust并未命名全部类型,它仅命名两种类型的部分不匹配。该函数接受&amp; mut c
和c
是匹配的通用部分。box
类型是用c
替换的,并且无法实现此特征。参考确实是
大小
,但是如上所述,我们与类型mut dyn redis :: aio :: connection like
相对于&amp; mut c 。这将导致
c
为dyn redis :: aio ::类似连接
。在Rust中,通用类型具有
大小
bond 默认情况下。您必须明确指定sized
bunditi此功能没有。因此,c
必须实现大小
和dyn redis :: aio :: connection like
否。,但不是直接的。您需要实现一种包装引用到
dyn Connection
> 或box&lt; dyn Connection&gt;
的类型。像:No, because the reference is not part of the generic type
C
. Rust doesn't name the entire type, it only names the parts of the two types that don't match. The function accepts&mut C
andC
is the generic part being matched. TheBox
type is what is substituted forC
, and it doesn't implement this trait.A reference is indeed
Sized
, but as noted above we are matching the type&mut dyn redis::aio::ConnectionLike
against&mut C
. This results inC
beingdyn redis::aio::ConnectionLike
.In Rust, generic types have the
Sized
bound by default. You have to explicitly specify the?Sized
bound to relax this, which the author of this function did not do. Therefore,C
must implementSized
, anddyn redis::aio::ConnectionLike
does not.Possibly, but not directly. You would need to implement a type that wraps either a reference-to-
dyn ConnectionLike
or aBox<dyn ConnectionLike>
and pass that instead. Something like:Rust中的所有类型参数均具有隐式
尺寸
绑定,除非它们选择退出 - 编译器的作用为c:sizied sized
已指定。可能会推广此代码以允许允许使用的类型(书面
c:?siped
);我不知道。是的,但是隐式界限是在指称上,
c
。,您需要定义或使用现有的类型,该类型实现了
connection like
特征,包含 a adyn Connection
,然后将可变的引用传递给该类型。最清洁的方法是通过实现实现来使用框本身,
但是,只有定义性状的板条箱才能添加
impl
。All type parameters in Rust have an implicit
Sized
bound unless they opt out — the compiler acts as ifC: Sized
was specified.Possibly this code could be generalized to allow unsized types (written
C: ?Sized
); I don't know.Yes, but the implicit bound is on the referent,
C
.You need to define, or use an existing, type which implements the
ConnectionLike
trait and contains adyn ConnectionLike
, then pass a mutable reference to that type.The cleanest approach is to use Box itself, by having the implementation
But, only the crate defining the trait can add that
impl
.