tokio-rs / prost

PROST! a Protocol Buffers implementation for the Rust Language
Apache License 2.0
3.78k stars 491 forks source link

Recursive oneof message cannot compile #1143

Open kvc0 opened 2 weeks ago

kvc0 commented 2 weeks ago

Expected: A message with a oneof can refer to a map of itself.

Observed: An opaque error message

error[E0596]: cannot borrow `value` as mutable, as it is not declared as mutable
  --> src/value_wrapper.rs:11:28
   |
11 | #[derive(Clone, PartialEq, prost::Oneof)]
   |                            ^^^^^^^^^^^^ cannot borrow as mutable
   |
   = note: this error originates in the derive macro `prost::Oneof` (in Nightly builds, run with -Z macro-backtrace for more info)

Source

use std::collections::HashMap;

#[derive(Clone, PartialEq, prost::Message)]
pub struct DiskValue {
    #[prost(oneof = "ValueKind", tags = "1")]
    pub kind: Option<ValueKind>,
}

#[derive(Clone, PartialEq, prost::Oneof)]
pub enum ValueKind {
    #[prost(map = "string, message", tag = "1")]
    Map(HashMap<String, DiskValue>),
}

I carefully referenced https://github.com/tokio-rs/prost/blob/master/tests/src/message_encoding.rs . This is likely PBKAC but I seem to be missing something here. I have no field named "value" and I'm not sure how to inspect this procedural macro. I use VS Code and Rust Analyzer doesn't have an option to expand the macro to show what's happening.

I'm not using a .proto. This is my source code for dependency prost = { version = "0.13" }

caspermeijn commented 2 weeks ago

I don't have experience with using derive prost::OneOf. If you model this in a .proto, does that work? Is the generated code any different from the example?