[personal profile] sassa_nf
Well, in the previous example it was an interesting case - the same map has been used to find a super class and a sub class in an inheritance tree, and Rust basically told us that it is not sure that the two are different objects. (So you can't assume you can modify the sub class without modifying the super class)

Another interesting example of a similar problem:
error[E0382]: borrow of moved value: `body`
   --> src/main.rs:194:19
    |
189 |          let mut body = self.file.as_mut().take(len as u64);
    |              -------- move occurs because `body` has type `std::io::Take<&mut dyn std::io::BufRead>`, which does not implement the `Copy` trait
...
194 |             while body.read(&mut preamble[0..1])? > 0 {
    |                   ^^^^ value borrowed here after move
...
234 |                let mut elem_body = body.take(len as u64);
    |                                    ---- value moved here, in previous iteration of loop


Here one stream is sliced into sub-streams of known length (body.take(...)) in a loop. Rust is telling us we can't refer to the parent stream after creating one slice.

Another curious problem that occurred several times:

Suppose I want to do something to a value, but choose different value depending on a condition. Like, read from stream A or stream B derived from A. Creating that second value only on one branch requires knowing what lifetime to assign. This is trivial, if the action to be done is on the same branch, but it is not clear how to do that, if I want the common action to be outside.

Like, (sketch):
   if ... {
      body = preamble[0..premable_sz].chain(&mut body);
   }
   body.read_exactly(...);
won't work, because Chain constructed in that branch has a lifetime of that branch, but must be used outside. One obvious (but ugly*) solution is to create a continuation and call it from both branches.

*ugly - I've seen where this ends up, if you follow it through methodically.

Date: 2020-01-16 03:52 pm (UTC)
juan_gandhi: (Default)
From: [personal profile] juan_gandhi
Now I'm kind of disappointed with using a continuation, too.
Still trying to figure out...

Profile

sassa_nf

January 2026

S M T W T F S
    123
45678910
111213141516 17
18192021222324
25262728293031

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 20th, 2026 11:35 pm
Powered by Dreamwidth Studios