Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Enums 如何实现枚举的借用、Toowed或Deref?_Enums_Rust - Fatal编程技术网

Enums 如何实现枚举的借用、Toowed或Deref?

Enums 如何实现枚举的借用、Toowed或Deref?,enums,rust,Enums,Rust,我有这样一种类型: enum Name<'a> { Single(&'a str), Double(&'a str, &'a str), } enum OwnedName { Single(String), Double(String, String), } impl ops::Deref for OwnedName { type Target = Name; fn deref(&self) -&g

我有这样一种类型:

enum Name<'a> {
    Single(&'a str),
    Double(&'a str, &'a str),
}
enum OwnedName {
    Single(String),
    Double(String, String),
}
impl ops::Deref for OwnedName {
    type Target = Name;

    fn deref(&self) -> &Name {
        match *self {
            OwnedName::Single(ref s) => Name::Single(&**s),
            OwnedName::Double(ref s1, ref s2) => Name::Double(&**s1, &**s2),
        }
    }
}
我已经有了将名称转换为OwnedName的方法。但是,对于这种类型,我无法找到一种方法来实现类似
Deref
Borrow
的功能,以便将OwnedName转换回名称。这意味着我必须编写两次相同的方法,这很烦人。我想要一些模仿
PathBuf
/
Path
String
/
str
工作方式的东西。我试过这样的方法:

enum Name<'a> {
    Single(&'a str),
    Double(&'a str, &'a str),
}
enum OwnedName {
    Single(String),
    Double(String, String),
}
impl ops::Deref for OwnedName {
    type Target = Name;

    fn deref(&self) -> &Name {
        match *self {
            OwnedName::Single(ref s) => Name::Single(&**s),
            OwnedName::Double(ref s1, ref s2) => Name::Double(&**s1, &**s2),
        }
    }
}

此错误包含
错误数量的生存期参数:预期为1,在
type Target=Name
上发现0,这是完全可以理解的,而且它需要生存期。但我不能提供一个。那么有没有一种方法可以让我这样使用
Deref
,或者
借用
,或者
ToOwned

请注意,
Deref::Deref
的签名表示它需要一个指向
名称
的指针,该指针的生命周期与被撤销的对象相同。这样的值不存在。您也不能返回指向在
deref
函数中创建的对象的指针,因为它不可能超过自己的堆栈帧

基本问题是
Name
基本上是一种不同类型的指针(比如
*const T
是指向
&T
),但是
Deref
和其他类似的特性只允许返回借用的指针。目前,这是无法避免的


但是,如果您不使用
Deref
和其他类似的特性,您就可以这样做。一种可能是
impl for Name我同意@DK。只需补充一点,如果您的想法是互换使用它们,您可能根本不需要拥有一个
OwnedName
。 您可以定义名称以接受
&str
字符串

enum Name<T> where T:Borrow<str> {
    Single(T),
    Double(T, T),
}

// these both work now
let _a1 = Name::Single("hello");
let _a2 = Name::Double(String::from("hello"), String::from("world"));
枚举名称,其中T:brook{
单个(T),
双(T,T),
}
//现在这两个都能用了
让_a1=Name::Single(“你好”);
让_a2=Name::Double(String::from(“hello”)、String::from(“world”);

事实上,可以从
Deref::Deref
返回函数内部创建的值:该值必须是胖指针。这就是
Deref
PathBuf
上的实现的工作原理-它返回
&Path
,这是指向未指定大小的
路径的胖指针。当然,这仅限于指向非大小类型的指针,不幸的是,不可能为
OwnedName
类型编写非大小的枚举等价物,因此这种方法在这种情况下不起作用。这是我试图模拟的
PathBuf
内部的
Path
的非大小化类型-如果我想这样做,我必须在这里使用
str
而不是
&str
,在我的特定枚举中我不能这样做。好主意,我没有想到。你的类型与
str
进行比较和散列吗?如果没有,您不能执行
借用
。这是一般规则还是您专门给我建议的?:)我无法比较和散列与
str
相同的字符串,因为我的一个变体中有两个字符串。这是
Borrow
impls需要遵守的规则(由Borrow记录)。我的意思是“绝对不应该”。