我有点卡住了(除了复制内存(请参阅playpen中的DupKeyEAB)),我得到了以下特征:
pub trait KeyVal : Send + Sync + 'static {
type Key : Clone + Debug + 'static;
fn get_key(&self) -> Self::Key;
fn get_key_ref<'a>(&'a self) -> &'a Self::Key;
}
我的问题是我需要类似的东西(以避免每次访问 key 时都使用克隆):
pub trait KeyVal : Send + Sync + 'static {
type Key : Clone + Debug + 'static;
type KeyRef<'_> : Debug;
fn get_key(&self) -> Self::Key;
fn get_key_ref<'a>(&'a self) -> Self::KeyRef<'a>;
}
或直接(以KeyRef为特征)
fn get_key_ref<'a,K : KeyRef<'a>>(&'a self) -> K;
或者
fn get_key_ref<'a>(&'a self) -> KeyRef<'a>;
所有这些表示法显然都是无效的,但它说明我需要返回与实现我的特征的结构具有相同生存期的引用,但与此同时,该引用不能只是&'a而是具有相同生存期的枚举。
这样,当在简单的结构上使用get_key_ref时,我的KeyRef就是&'aKey,当在枚举上使用get_key_ref并结合了多种特征实现(请参见EnumAB)时,我可以在对键的引用上使用包装枚举:像
KeyABRef<'a>
。 impl EnumAB {
fn get_key_ok<'a>(&'a self) -> KeyABRef<'a> {
match self {
&EnumAB::A(ref a) => KeyABRef::A(a.get_key_ref()),
&EnumAB::B(ref b) => KeyABRef::B(b.get_key_ref()),
}
}
}
但是我无法将此功能纳入我的特征。
我想知道是否有人针对这种需求找到了解决方案(KeyVal必须为“静态”)?
我原来的测试代码是:
use std::fmt::Debug;
use std::thread;
pub trait KeyVal : Send + Sync + 'static {
type Key : Clone + Debug + 'static;
fn get_key(&self) -> Self::Key;
fn get_key_ref<'a>(&'a self) -> &'a Self::Key;
}
pub fn do_something_with_spawn<KV : KeyVal> (kv : KV) {
thread::spawn ( move || {
println!("{:?}", kv.get_key_ref());
});
}
#[derive(Debug)]
pub struct StructA (usize);
#[derive(Debug)]
pub struct StructB (String);
#[derive(Debug)]
pub enum EnumAB {
A(StructA),
B(StructB),
}
impl KeyVal for StructA {
type Key = usize;
// type KeyRef<'_> = &'_ usize;
fn get_key(&self) -> Self::Key {
self.0.clone()
}
fn get_key_ref<'a>(&'a self) -> &'a Self::Key {
&self.0
}
}
impl KeyVal for StructB {
type Key = String;
// type KeyRef<'_> = &'_ String;
fn get_key(&self) -> Self::Key {
self.0.clone()
}
fn get_key_ref<'a>(&'a self) -> &'a Self::Key {
&self.0
}
}
#[derive(Clone,Debug)]
pub enum KeyAB {
A(usize),
B(String),
}
#[derive(Clone,Debug)]
pub enum KeyABRef<'a> {
A(&'a usize),
B(&'a String),
}
impl KeyVal for EnumAB {
type Key = KeyAB;
// type KeyRef<'_> = KeyABRef<'_>
fn get_key(&self) -> Self::Key {
match self {
&EnumAB::A(ref a) => KeyAB::A(a.get_key()),
&EnumAB::B(ref b) => KeyAB::B(b.get_key()),
}
}
fn get_key_ref<'a>(&'a self) -> &'a Self::Key {
panic!("cannot");
}
}
impl EnumAB {
fn get_key_ok<'a>(&'a self) -> KeyABRef<'a> {
match self {
&EnumAB::A(ref a) => KeyABRef::A(a.get_key_ref()),
&EnumAB::B(ref b) => KeyABRef::B(b.get_key_ref()),
}
}
}
#[derive(Debug)]
pub struct DupKeyEAB (KeyAB, EnumAB);
impl KeyVal for DupKeyEAB {
type Key = KeyAB;
fn get_key(&self) -> Self::Key {
self.0.clone()
}
fn get_key_ref<'a>(&'a self) -> &'a Self::Key {
&self.0
}
}
fn main () {
let d2 = StructB("hello".to_string());
let d3 = EnumAB::A(StructA(3));
println!("{:?}",d2.get_key());
println!("{:?}",d3.get_key());
println!("{:?}",d2.get_key_ref());
println!("{:?}",d3.get_key_ok());
do_something_with_spawn(d3);
}
最佳答案
我同意A.B.您的动机尚不清楚,因此我们能提供的任何帮助充其量都是在猜测。话虽如此,这是使用 Borrow
的猜测:
use std::borrow::Borrow;
trait KeyVal<'a> {
type Key: Borrow<Self::KeyRef>;
type KeyRef: 'a;
fn get_key(&self) -> Self::Key;
fn get_key_ref(&'a self) -> &'a Self::KeyRef;
}
struct Example(u8);
impl<'a> KeyVal<'a> for Example {
type Key = u8;
type KeyRef = u8;
fn get_key(&self) -> Self::Key {
self.0
}
fn get_key_ref(&'a self) -> &'a Self::KeyRef {
&self.0
}
}
fn main() {
let e = Example(42);
println!("{:?}", e.get_key());
println!("{:p}", e.get_key_ref());
}
另一个是
type KeyRef: 'a
,它指示选择的类型必须超过'a
。我想您虽然不需要
Borrow
:trait KeyVal<'a> {
type Key;
type KeyRef: 'a;
fn get_key(&self) -> Self::Key;
fn get_key_ref(&'a self) -> Self::KeyRef;
}
#[derive(Debug)]
struct Example(u8);
#[derive(Debug)]
struct RefWrapper<'a>(&'a u8);
impl<'a> KeyVal<'a> for Example {
type Key = u8;
type KeyRef = RefWrapper<'a>;
fn get_key(&self) -> Self::Key {
self.0
}
fn get_key_ref(&'a self) -> Self::KeyRef {
RefWrapper(&self.0)
}
}
fn main() {
let e = Example(42);
println!("{:?}", e.get_key());
println!("{:?}", e.get_key_ref());
}