2 ms·
Awesome! I wonder if instead of using a boolean field in the Segment struct, better results might be attained by using a tagged union (enum in Rust-speak):
by dikaiosune 10y ago
Awesome!
I wonder if instead of using a boolean field in the Segment struct, better results might be attained by using a tagged union (enum in Rust-speak):
struct Point {
x: f64, y: f64
}
enum Segment {
Degenerate {
p1: Point,
},
Full {
p1: Point,
p2: Point,
p3: Point,
p4: Point,
}
}
impl Segment {
fn upgrade(&mut self,
p2: Point,
p3: Point,
p4: Point) {
match *self {
Segment::Degenerate { p1 } => {
*self = Segment::Full {
p1: p1,
p2: p2,
p3: p3,
p4: p4,
};
}
_ => ()
}
}
}
I wonder if you could also get away without using the has_first_segment variable by using the Vec::len method:
if segments.len() > 0 {
segment_number += 1;
}
Of course, that's assuming the author doesn't find a better way to represent the pattern of accessing the previous, half-built segment. I suspect you could perhaps do so by creating a mutable partial Segment on the stack from an iterator's first element, and then actually looping over the remaining items in the iterator to actually construct segments, adding them to the Vec as you go.
EDIT: It doesn't look like this thread is going anywhere, but I saw a comment on the Rust subreddit to use a Point struct which I quite liked, and incorporated here.