0

We have the following situation:

#[derive(Debug, Clone, PartialEq)]
pub struct Bar{
pub name: String,
pub anotherbar: Box<Option<Bar>>,
pub customenum: Option<Vec<Enum>>,
pub yesno: bool,
}

#[derive(Debug, Clone, Copy)]
pub struct foo {
pub name: String,
pub vector: Vec<bar>,
pub tuple: Vec<(u8, Bar)>,
}

and get the error message:

error[E0204]: the trait `Copy` may not be implemented for this type
   --> src/types.rs:459:24
    |
459 | #[derive(Debug, Clone, Copy)]
    |                        ^^^^
460 | pub struct Deck{
461 |     pub name: String,
    |     ---------------- this field does not implement `Copy`
462 |     pub commander: Vec<Card>,
    |     ------------------------ this field does not implement `Copy`
463 |     pub library: Vec<(u8, Card)>,
    |     ---------------------------- this field does not implement `Copy`
    |
    = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0204`.

I want to use the struct bar in a parallelization situation, where multiple requests add to the tuple field.

Can anyone help and explain?

2
  • Sorry the fields differ here because I took the error message from the compiler. Commented Dec 29, 2021 at 20:29
  • I don't see any tuple field in Bar. Perhaps you meant struct foo. See why you cannot implement Copy on your type Commented Dec 29, 2021 at 20:39

1 Answer 1

2

Copy indicates "Types whose values can be duplicated simply by copying bits." (As a practical matter, Copy also strongly implies that copying the type is very cheap and so can be done implicitly.) Vec and String cannot be copied this way due to their internal buffers. They are both Clone (in the case of Vec, only when its contents are also Clone). Clone means that a copy is possible, but must be done explicitly using the .clone() call.

I want to use the struct bar in a parallelization situation, where multiple requests add to the tuple field.

This sounds like you're trying to create shared mutable state. You should first carefully consider if this is what you really want. Typically you would instead do work in parallel and then collect all the data together at the end. That way the parallel activities don't interfere with each other. But if they must work together, then you'll need to use Mutex and other tools to provide thread-safety.

In either case, Copy wouldn't help you there, since that would create independent copies of the struct, which seems the opposite of what you're describing.

For more on Copy, see the docs explaining it.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for your quick and accurate answer. I just have a program that fetches data via HTTPS-request and builds something from it. The request is my bottleneck and I try to parallelize it. Therefore I go through different possibilities like std::thread and Rust async. The background is to build a repository that can be shown by applications on job positions. Idk what is too much and what fits my plan. I try to play around and build the program with different technics to get a good overview. Overall I am fairly new to Rust and just have little experience with C.
What you're describing is a classic producer/consumer system. The tool you'll want is mpsc::channel. See the Rust book for a nice tutorial on building a multi-threaded web server (you want a web client, but the principles are the same, just reversed). doc.rust-lang.org/book/ch20-00-final-project-a-web-server.html I highly recommend reading the entire Rust book. Rust is not actually that hard a language, but it is not like any other language I've worked in (about 2 dozen of them). You need to spend a little time learning how Rust approaches problems or you'll fight it all day.
That's fair. I just read and parallel programmed problems for and with the Rust book. But still, it's hard to understand all those interactions. The program I mentioned before is already complete, just not good. It works the way intended, but friends pointed out that it is less "rust like" as it is supposed to be. They suggested just to start over and try it again. Therefore I try now. However, whenever trying new concepts or stuff I do not really get the basics applied to the problem. I feel like often do not quite see through things. Will become better over time.
I already tried to work with mpsc::channel. Building those structures on your own appears very hard for me. Therefore I was let to rayon and crossbeam. Async seems to be a very hard but fancy way to solve the problem.
Thank you for your support. After all, it worked out. I implemented 4 threads and used Arc to clone the data and move it into the threads. It took a piece of advice to drop the main sender.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.