Skip to content

Commit c856547

Browse files
authored
Merge pull request aeron-io#518 from ZackPierce/simplify_done_output
[Rust] Simplify done unwrapping to prevent dual mutable aliasing
2 parents 2d49909 + 66ecdfd commit c856547

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

rust/car_example/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ fn decode_car_and_assert_expected_content(buffer: &[u8]) -> CodecResult<()> {
139139
println!("Activation Code: {}", activation_code);
140140
assert_eq!("abcdef", activation_code);
141141

142-
let (position, buffer_back) = dec_done.unwrap();
142+
let position = dec_done.unwrap();
143143
println!("Finished decoding. Made it to position {0} out of {1}",
144144
position,
145-
buffer_back.len());
145+
buffer.len());
146146
Ok(())
147147
}
148148

@@ -202,7 +202,7 @@ fn encode_car_from_scratch() -> CodecResult<Vec<u8>> {
202202
let enc_model = enc_manufacturer.manufacturer("Honda".as_bytes())?;
203203
let enc_activation_code = enc_model.model("Civic VTi".as_bytes())?;
204204
let done = enc_activation_code.activation_code("abcdef".as_bytes())?;
205-
let (pos, _) = done.unwrap();
205+
let pos = done.unwrap();
206206
pos
207207
};
208208
println!("encoded up to position {}", used_pos);

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustCodecType.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,10 @@ String generateDoneCoderType(
118118
{
119119
appendScratchWrappingStruct(writer, doneTypeName);
120120
RustGenerator.appendImplWithLifetimeHeader(writer, doneTypeName);
121-
writer.append(INDENT).append(String.format("pub fn unwrap(mut self) -> (usize, &%s%s [u8]) {\n",
122-
DATA_LIFETIME, this == Encoder ? " mut" : ""))
123-
.append(INDENT).append(INDENT).append(format("(self.%s.pos, self.%s.data)\n",
124-
scratchProperty(), scratchProperty()))
125-
.append(INDENT).append("}\n");
121+
indent(writer, 1, "/// Returns the number of bytes %s\n", this == Encoder ? "encoded" : "decoded");
122+
indent(writer, 1, "pub fn unwrap(self) -> usize {\n");
123+
indent(writer, 2, "self.%s.pos\n", scratchProperty());
124+
indent(writer, 1, "}\n");
126125

127126
appendWrapMethod(writer, doneTypeName);
128127
writer.append("}\n");

0 commit comments

Comments
 (0)