Generics 处理需要调整大小的特征对象

Generics 处理需要调整大小的特征对象,generics,rust,traits,Generics,Rust,Traits,我想要一个trait对象包装结构的LinkedList。内部将是Ssl或非Ssl流的流类型。我的希望是传递struct包装器,只要内部符合相同的特性,无论使用什么内部流类型,一切都会正常 简单的例子: use std::sync::{Arc, Mutex}; use std::collections::LinkedList; use std::os::unix::io::{RawFd, AsRawFd}; pub trait HRecv {} pub trait HSend {} pub tr

我想要一个trait对象包装结构的
LinkedList
。内部将是Ssl或非Ssl流的流类型。我的希望是传递struct包装器,只要内部符合相同的特性,无论使用什么内部流类型,一切都会正常

简单的例子:

use std::sync::{Arc, Mutex};
use std::collections::LinkedList;
use std::os::unix::io::{RawFd, AsRawFd};

pub trait HRecv {}
pub trait HSend {}
pub trait HStream: HRecv + HSend + AsRawFd + Clone {}
pub struct Stream<T: HStream> {
    pub inner: T
}

pub type StreamList = Arc<Mutex<LinkedList<Stream<HStream>>>>;

fn main() {
    let mut list = Arc::new(Mutex::new(LinkedList::<Stream<HStream>>::new()));
}
使用std::sync::{Arc,Mutex};
使用std::collections::LinkedList;
使用std::os::unix::io:{RawFd,AsRawFd};
pub-HRecv{}
发布特征HSend{}
发布特征HStream:HRecv+HSend+AsRawFd+Clone{}
发布结构流{
酒吧内景:T
}
pub-type-StreamList=Arc;
fn main(){
让mut list=Arc::new(Mutex::new(LinkedList:::new());
}
产生以下错误:

error: the trait 'core::marker::Sized' is not implemented for the type 'HStream' [E0277]
let mut list = Arc::new(Mutex::new(LinkedList::<Stream<HStream>>::new()));
                                                ^~~~~~~~~~~~~~~
错误:没有为类型“HStream”[E0277]实现特征“core::marker::Sized”
让mut list=Arc::new(Mutex::new(LinkedList:::new());
^~~~~~~~~~~~~~~
我尝试将
+size
添加到
HStream
的定义中,并将
内部
设置为
,两者都会产生相同的错误


目前是否有可能对铁锈进行处理?如果是,语法是什么?

好的,这里有一些问题。确定编译器错误列表:

<anon>:15:53: 15:68 error: the trait `core::marker::Sized` is not implemented for the type `HStream` [E0277]
<anon>:15     let mut list = Arc::new(Mutex::new(LinkedList::<Stream<HStream>>::new()));
                                                              ^~~~~~~~~~~~~~~
<anon>:15:53: 15:68 help: see the detailed explanation for E0277
<anon>:15:53: 15:68 note: `HStream` does not have a constant size known at compile-time
<anon>:15:53: 15:68 note: required by `Stream`
一种特质不能自我实现。您要求的类型实现了
HStream
,但是
HStream
本身没有实现(它将如何实现?)

您必须提供一个类型


您不能直接使用
HStream
类型;它不代表任何东西。它仅用于构造派生指针类型,例如
&HStream
Box

最简单的解决方案是使用
流的
链接列表

fn main(){
让mut list=Arc::new(Mutex::new(LinkedList:::new());
}
然后,我们只需为
Box
实现
HStream

impl{}
impl{}
恳求{
fn as_raw_fd(&self)->RawFd{(&**self).as_raw_fd()}
}
impl{}
请注意,这缺少一个特征<代码>克隆

克隆
不是对象安全的,这意味着无法为该特征创建特征对象类型,例如
&Clone
Clone
不是对象安全的,因为它的
Clone
方法返回
Self
,它表示实现者的具体类型。如果通过trait对象使用此方法,编译器将无法提前知道结果的类型(它可能是任何
Clone
的实现者!)

由于
HStream
Clone
的子序列,
HStream
也不是对象安全的。结果是我们根本无法实现
Clone
,像
Box
这样的类型是不合法的

然而,我们可以通过制作自己的对象安全特性来解决这个问题。我们甚至可以在实现标准
Clone
trait的类型上自动实现它

pub trait BoxedHStreamClone{
fn盒装克隆(&self)->Box;
}
//实现所有实现HStream和Clone且不持有任何借用的类型
impl,无法实现克隆
恳求{
fn已装箱的\u克隆(&self)->Box{
Box::新建(&**self.boxed_clone())作为Box
}
}
HStream
上绑定的
Clone
特征替换为
BoxedHStreamClone
,您就可以开始了

pub-trait-HStream:HRecv+HSend+AsRawFd+BoxedHStreamClone{}

使用std::sync::{Arc,Mutex};
使用std::collections::LinkedList;
使用std::os::unix::io:{RawFd,AsRawFd};
pub-trait-BoxedHStreamClone{
fn盒装克隆(&self)->Box;
}

impl HRecv for Box HSend for Box AsRawFd for Box Box stream clone for Box HStream for Box感谢您花时间回答,两个答案得出了相同的结论,所以我选择了第一个提交的答案。再次感谢:)
<T: ?Sized + HStream>

<anon>:15:53: 15:68 error: the trait `HStream` is not implemented for the type `HStream` [E0277]
<anon>:15     let mut list = Arc::new(Mutex::new(LinkedList::<Stream<HStream>>::new()));
                                                              ^~~~~~~~~~~~~~~
<anon>:15:53: 15:68 help: see the detailed explanation for E0277
<anon>:15:53: 15:68 note: required by `Stream`
<anon>:15:53: 15:68 error: the trait `HStream` cannot be made into an object [E0038]
<anon>:15     let mut list = Arc::new(Mutex::new(LinkedList::<Stream<HStream>>::new()));
                                                              ^~~~~~~~~~~~~~~
<anon>:15:53: 15:68 help: see the detailed explanation for E0038
<anon>:15:53: 15:68 note: the trait cannot require that `Self : Sized`