Rust 用泛型混淆特征继承

Rust 用泛型混淆特征继承,rust,Rust,我开始为一个新图书馆玩弄锈蚀。我正试图用我的头脑来思考实现以下内容的可能方法 接下来是更多的期望表达式,而不是真正的语法。我试图表达这一点的所有方法要么不编译,要么在我实现一个别名特性时不编译 struct ConcreteType; struct CommonType; trait Handler<Rin, Rout = Rin>{ fn handle_event(&self, msg: &Rin); } // alias Handler with on

我开始为一个新图书馆玩弄锈蚀。我正试图用我的头脑来思考实现以下内容的可能方法

接下来是更多的期望表达式,而不是真正的语法。我试图表达这一点的所有方法要么不编译,要么在我实现一个别名特性时不编译

struct ConcreteType;
struct CommonType;

trait Handler<Rin, Rout = Rin>{
    fn handle_event(&self, msg: &Rin);
}

// alias Handler with one of the types defined as a common case
trait HandlerToMessage<M> : Handler <ConcreteType, M>{
    fn handle_event(&self, msg: &ConcreteType) {
        // default implementation of parent trait
        // example is simplified, forget about how Rout/M is actually used
        self.decode(msg)
    }

    // method to implement
    fn decode(&self, msg: &ConcreteType) -> M;
}

// another alias for most common case where Rin/Rout are ConcreteType, CommonType most often
trait HandlerToCommonType : HandlerToMessage <ConcreteType, CommonType>{
    fn decode(&self, msg: &ConcreteType) -> CommonType 
    {
       ...
    };
}
struct-ConcreteType;
结构公共类型;
特质处理者{
fn处理事件(&self,msg:&Rin);
}
//别名处理程序,其中一种类型定义为常见情况
特征句柄消息:句柄{
fn handle_事件(&self,msg:&ConcreteType){
//父trait的默认实现
//这个例子很简单,忘记Rout/M实际上是如何使用的
自我解码(msg)
}
//实现方法
fn解码(&self,msg:&ConcreteType)->M;
}
//Rin/Rout为ConcreteType的最常见情况的另一个别名,通常为CommonType
特征HandlerToCommonType:HandlerToMessage{
fn解码(&self,msg:&ConcreteType)->CommonType
{
...
};
}
使用关联类型的替代方法

trait Handler{
    type Rin;
    type Rout;    // not yet able to do Rout = Rin with associated types

    fn handle_event(&self, msg: &Self::Rin) -> Self::Rout;
}


trait HandlerToMessage : Handler <Rin=ConcreteType>{
    fn handle_event(&self, msg: &Self::Rin) {
        // common functionality
        self.decode(msg)
    }

    // method to implement
    fn decode(&self, msg: &Self::Rin) -> Self::Rout;
}

trait HandlerToCommonType : HandlerToMessage <Rout=CommonType>{

    fn decode(&self, msg: &ConcreteType) -> CommonType
    {
        ...
    } 
}
trait处理程序{
Rin型;
类型Rout;//还不能使用关联的类型执行Rout=Rin
fn handle_事件(&self,msg:&self::Rin)->self::Rout;
}
特征句柄消息:句柄{
fn handle_事件(&self,msg:&self::Rin){
//通用功能
自我解码(msg)
}
//实现方法
fn解码(&self,msg:&self::Rin)->self::Rout;
}
特征HandlerToCommonType:HandlerToMessage{
fn解码(&self,msg:&ConcreteType)->CommonType
{
...
} 
}
< C++ >这是我想完成的

// real world example I've seen in the wild of this structure
template <class Rout>
class Context {
public:
    void dispatch(Rout* msg);
};

template <class Rin, Rout = Rin>
class ReadHandler {
public:
    void read (Context* ctx, Rin* msg) = 0;

private:
    Context<Rout> ctx_;
};

// very common to convert from a byte buffer some message type
template <class M>
class BytesToMessageDecoder : ReadHandler<IOBuffer, M> {
public:
    // Template method pattern
    void read (Context* ctx, IOBuffer* msg) {
        M msgOut;
        bool success;
        success = this->decode(msg, &msgOut);
        if (success) {
            ctx->dispatch(msgOut);
        }                
    }

    bool decode(IOBuffer* msg, M* msgOut) = 0;

}

// convert one byte buffer to another is common
typedef BytesToMessageDecoder<IOBuffer> BytesToBytesDecoder;


// Concrete implementations
// look for fixed number of bytes incoming
class FixedLengthFrameDecoder : BytesToBytesDecoder {
    bool decode(IOBuffer* msg, IOBuffer* msgOut) { ... }
}

// fields are prefixed with a length. Wait for that many bytes and then dispatch
class LengthBasedFieldDecoder: BytesToBytesDecoder {
    bool decode(IOBuffer* msg, IOBuffer* msgOut) { ... }
}

class StringDecoder : BytesToMessageDecoder<std::string> { 
    // decode from byte buffer to a string
    bool decode(IOBuffer* msg, std::string* msgOut) { ... }
}
//我在这个结构的野外看到的真实例子
模板
类上下文{
公众:
无效调度(路由*消息);
};
模板
类ReadHandler{
公众:
无效读取(上下文*ctx,Rin*msg)=0;
私人:
上下文ctx;
};
//从字节缓冲区转换某些消息类型非常常见
模板
类BytesToMessageDecoder:ReadHandler{
公众:
//模板方法模式
无效读取(上下文*ctx,IOBuffer*msg){
M-痛风;
成功;
成功=此->解码(msg和msgOut);
如果(成功){
ctx->调度(msgOut);
}                
}
bool解码(IOBuffer*msg,M*msgOut)=0;
}
//将一个字节缓冲区转换为另一个字节缓冲区是常见的
typedef BytesToMessageDecoder BytesToBytesDecoder;
//具体实施
//查找传入的固定字节数
类FixedLengthFrameDecoder:ByTestObjetsCoder{
bool解码(IOBuffer*msg,IOBuffer*msgOut){…}
}
//字段以长度作为前缀。等待这么多字节,然后分派
类LengthBasedFieldDecoder:ByTestObjetsCoder{
bool解码(IOBuffer*msg,IOBuffer*msgOut){…}
}
类StringDecoder:BytesToMessageDecoder{
//从字节缓冲区解码为字符串
bool解码(IOBuffer*msg,std::string*msgOut){…}
}
基本上,顶级trait
处理程序
是最通用的,但除了高级库用户之外,可能不会被任何人实现。
handlertemessage
特性是一种常见的转换,我们将
ConcreteType
转换为其他类型。库可以实现其中的几个。
HandlerToCommonType
是许多库类型希望从中开始的最常见的情况

关于如何在
处理程序
特征中使用
Rout
的详细信息并不重要。我试图简化这个例子,并省略了一些论点,希望能使我试图表达的内容更加简洁。我在这方面的所有搜索要么让我认为这是不可能传达的,要么我滥用了它。我不太明白这是否属于新的专门化实现,但从我的理解来看,感觉不是这样


<>我意识到锈不是C++,所以也许我想做的是不支持或者有不同的语法。无论是以正确的语法还是更惯用的方式,我们都非常感谢您的帮助。

也许您可以拥有单独的特性,并为其他的所有实现者实现一个特性:

struct ConcreteType;
struct CommonType;

trait Handler<Input, Output = Input> {
    fn handle_event(&self, msg: &Input) -> Output;
}

trait HandlerToMessage<M> {
    fn decode(&self, msg: &ConcreteType) -> M;
}

impl<T, M> Handler<ConcreteType, M> for T
    where T: HandlerToMessage<M>
{
    fn handle_event(&self, msg: &ConcreteType) -> M {
        self.decode(msg)
    }
}

impl HandlerToMessage<CommonType> for () {
    fn decode(&self, _msg: &ConcreteType) -> CommonType {
        unimplemented!()
    }
}

fn main() {}
struct-ConcreteType;
结构公共类型;
特质处理者{
fn handle_事件(&self,msg:&Input)->输出;
}
特征信息{
fn解码(&self,msg:&ConcreteType)->M;
}
T的impl处理程序
其中T:HandlerToMessage
{
fn handle_事件(&self,msg:&ConcreteType)->M{
自我解码(msg)
}
}
()的impl HandlerToMessage{
fn解码(&self,_msg:&ConcreteType)->CommonType{
未执行!()
}
}
fn main(){}

最后一个是非常尴尬的,因为您通常会为具体类型实现一个trait,但您没有真正提出任何有意义的实现方法。

也许您可以只拥有单独的trait,并为其他所有实现者实现一个trait:

struct ConcreteType;
struct CommonType;

trait Handler<Input, Output = Input> {
    fn handle_event(&self, msg: &Input) -> Output;
}

trait HandlerToMessage<M> {
    fn decode(&self, msg: &ConcreteType) -> M;
}

impl<T, M> Handler<ConcreteType, M> for T
    where T: HandlerToMessage<M>
{
    fn handle_event(&self, msg: &ConcreteType) -> M {
        self.decode(msg)
    }
}

impl HandlerToMessage<CommonType> for () {
    fn decode(&self, _msg: &ConcreteType) -> CommonType {
        unimplemented!()
    }
}

fn main() {}
struct-ConcreteType;
结构公共类型;
特质处理者{
fn handle_事件(&self,msg:&Input)->输出;
}
特征信息{
fn解码(&self,msg:&ConcreteType)->M;
}
T的impl处理程序
其中T:HandlerToMessage
{
fn handle_事件(&self,msg:&ConcreteType)->M{
自我解码(msg)
}
}
()的impl HandlerToMessage{
fn解码(&self,_msg:&ConcreteType)->CommonType{
未执行!()
}
}
fn main(){}

最后一个很尴尬,因为你通常会为一个具体类型实现一个特性,但是你没有真正的提出任何实现它的意义。

你的第一个例子根本不使用<代码> Ruo> <代码>。@ SePiMeST我编辑C++示例更加现实。我希望,因为我不是肯定的,我可以用锈来表达这个想法。如果我能说得更清楚,请告诉我。谢谢你的支持,非常感谢。你的第一个例子根本不使用<代码> RUT/<代码> @ @ SeMeMeb我把C++示例编辑成B