gpt4 book ai didi

Copy an object that can't implement Copy(复制无法实现复制的对象)

转载 作者:bug小助手 更新时间:2023-10-26 20:24:07 29 4
gpt4 key购买 nike



I have this section of code where I need to figure out the type of an encoded value and I don't know if it is a String, an unsigned integer, or a Vector of String. I thought of doing the following:

我有一段代码,我需要确定编码值的类型,但我不知道它是字符串、无符号整数还是字符串的矢量。我想做以下几件事:


while let Some(pair) = dict_dec.next_pair()? {
match pair {
...
// &[u8] Object<'_, '_>
(unknown_field, value) => {
let field_name = String::from_utf8_lossy(unknown_field).to_string();
let value_as_string: String = match String::decode_bencode_object(value) {
Ok(s) => s,
Err(_) => match u64::decode_bencode_object(value) {
Ok(u) => u.to_string(),
Err(_) => match Vec::<String>::decode_bencode_object(value) {
Ok(v) => v.join(", "),
Err(_) => "Unknown value".to_string(),
},
},
};

let tuple: (String, String) = (field_name, value_as_string);
......

}
}


However value from the bendy crate can't implement Copy, it is moved after calling decode_bencode_object, and decode_bencode_object doesn't take a reference. Is there a way I can call it multiple times, make a copy or a different approach that would let me get the same result?

然而,来自弯曲板条箱的值不能实现复制,它在调用Decode_Bencode_Object之后被移动,并且Decode_Bencode_Object不接受引用。有没有一种方法,我可以多次调用它,复制一份,或者另一种方法,让我得到相同的结果?


更多回答

I have no idea what this complex decoding scheme has to do with copying, but to answer the title question, u32, String, and Vector<String> all implement Clone, which means you can call .clone() to copy them. The call has to be explicit, since copying vectors and strings is more complex than a mere bitcopy, so Rust doesn't want it happening behind-the-scenes.

我不知道这个复杂的解码方案与复制有什么关系,但为了回答标题问题,u32、字符串和向量<字符串>都实现了Clone,这意味着您可以调用.Clone()来复制它们。调用必须是显式的,因为复制向量和字符串比单纯的位复制更复杂,所以Rust不希望它在幕后发生。

I am not trying to copy a u32, a String or a Vector, but the variable value that I use as an argument for decode_bencode_object. value is an object from the bendy crate and doesn't implement copy, however I want to pass it three times as an argument to try to decode it as a String, as a int, or as a Vector.

我不是要复制u32、字符串或向量,而是要复制我用作DECODE_BENCODE_OBJECT参数的变量值。值是来自弯曲板条箱的对象,并且不实现复制,但是我想将其作为参数传递三次,以尝试将其解码为字符串、整型或向量。

I think it would help to edit your question to include the full error message you saw when you tried this, and information about which library this decode_bencode_object function belongs to, so it's clearer what the problem is and what the constraints on the solution might be.

我认为编辑您的问题以包括您在尝试此操作时看到的完整错误消息,以及有关此decode_bencode_Object函数属于哪个库的信息会有所帮助,这样就可以更清楚地了解问题所在以及解决方案可能受到的约束。

优秀答案推荐

I'm inferring from your question that you are using the FromBencode trait from crate bendy and so therefore these values are of type Object. Since FromBencode takes ownership of the Object it's given, you cannot continue to use value after you've passed it to one type's implementation of this trait.

我从您的问题中推断,您使用的是Crate Bendy中的FromBencode特征,因此这些值是Object类型。因为FromBencode拥有它所指定的对象的所有权,所以在将值传递给该特征的一个类型的实现之后,您将不能继续使用它。


The smallest change from your current program would be to call the clone method to create a deep copy of your Object for each call to decode_bencode_object. That would mean that you'd be moving the copy into the method and so you'd retain ownership of the original object. However, you will need to pay the cost of cloning the Object value potentially multiple times, so you should consider whether that cost is acceptable for your use-case.

与当前程序的最小更改是调用克隆方法,以便为每次调用decode_bencode_Object创建对象的深层副本。这意味着您将把副本移到方法中,因此您将保留原始对象的所有权。但是,您可能需要支付多次克隆对象值的成本,因此您应该考虑该成本对于您的用例是否可接受。


Another possibility would be to decide which type to try before you call the trait method. I'm not familiar with this library so I'm guessing a little but it seems like you could make some inferences based on which variant of enum Object is selected, like this:

另一种可能是在调用特征方法之前决定尝试哪种类型。我不熟悉这个库,所以我猜有一点,但您似乎可以根据选择的枚举对象的变体做出一些推断,如下所示:


match &value {
Object::Bytes(_) => {
// use String::decode_bencode_object
}
Object::Integer(_) => {
// use u64::decode_bencode_object
}
Object::List(_) => {
// use Vec::<String>::decode_bencode_object
}
}

The match expression uses a borrow of value so that you can retain ownership of it for use inside each of the three match arms. Each arm only needs to call one decode_bencode_object method, so this should be acceptable as long as you don't try to use value again after the match block.

匹配表达式使用值的借用,以便您可以保留它的所有权,以便在三个匹配臂中的每一个中使用。每个ARM只需要调用一个Decode_Bencode_Object方法,所以只要在Match块之后不尝试再次使用VALUE,这应该是可以接受的。


更多回答

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com