指定为特征而不是具体类型的可选函数参数 [英] Optional function argument that is specified as a trait instead of a concrete type

查看:100
本文介绍了指定为特征而不是具体类型的可选函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几个月我一直在观看Rust,但是我才刚刚开始进行实际的项目.我不确定标题中的术语是否正确.请让我知道如何纠正它.

I have been watching Rust for the past few months but I just started into an actual project. I am not sure if the terminology in the title is correct. Please let me know how it can be corrected.

我正在围绕ENet库( http://enet.bespin.org )编写防锈包装.我的目标是保持rust API与C API尽可能相似,除了重构将C样式句柄指针带入结构对象的成员函数的函数.我想使API保持相似,以便正式的C文档同样适用于rust wrapper.

I am writing a rust wrapper around the ENet library (http://enet.bespin.org). My goal is to keep the rust API as similar to the C API as possible except to refactor functions that take C style handle pointers into member functions of structure objects. I want to keep the API similar so that the official C documentation will apply equally well to the rust wrapper.

ENet公开了一个用于创建客户端主机或服务器主机的功能.创建服务器时,您将指向IP地址结构的指针传递给该函数.创建客户端时,您传递NULL.

ENet exposes a single function to create either a client host or a server host. When creating a server you pass a pointer to an IP Address structure to the function. When creating a client you pass NULL.

我正在尝试使用ToSocketAddr特质和Option来模仿这种行为,但是我在结合使用它们时遇到了问题.

I am trying to emulate that behavior using the ToSocketAddr trait and Option but I am running into problems using them in conjunction.

这是我要做的事情的简化示例:

This is a reduced example of what I am trying to do:

use std::io::net::ip::ToSocketAddr;

fn create_host<A: ToSocketAddr>(addr: Option<A>) {
    match addr {
        Some(a) => println!("Address is {}. Return a server host object.",a.to_socket_addr()),
        None    => println!("no address... Return a client.")
    };
}


fn main() {
    create_host(Some("localhost:12345"));
    create_host(None);
}

create_host()的第一次调用就像一个超级按钮.但是,第二个调用将无法编译.

The first call to create_host() works like a charm. The second call however will not compile.

Rustc返回

error: unable to infer enough type information about `_`; type annotations required

我猜正在发生错误,因为None没有为通用A提供任何解决方案.我尝试了以下操作,但由于ToSocketAddr没有实现特征core::kinds::Sized,因此无法正常工作.

I am guessing that error is occurring because None doesn't provide any resolution to the generic A. I tried the following but this doesn't work either because ToSocketAddr does not implement the trait core::kinds::Sized.

fn create_host(addr: Option<ToSocketAddr>) {
    ...
}

有没有办法做到这一点,或者我需要采取其他方法吗?

Is there a way I can do this or do I need to take a different approach?

推荐答案

fn main() {
    create_host(Some("localhost:12345"));
    create_host(None::<&str>);
}

我在这里选择了&str,因为它与第一次调用时的类型相同,因此编译器不会生成泛型函数的另一个单态化版本.您可以选择实现ToSocketAddr的任何类型.

I've chosen &str here as it is the same type as on the first call, so that the compiler wouldn't generate another monomorphized version of the generic function. You could choose any type that implements ToSocketAddr.

这篇关于指定为特征而不是具体类型的可选函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆