code_gen/rust/function/
with_result.rs

1use crate::rust::RustType;
2use crate::{CodeBuffer, Expression};
3
4/// An element with an optional result.
5pub trait WithResult: Sized {
6    /// Gets the optional result.
7    fn result(&self) -> Option<&RustType>;
8
9    /// Sets the `result`.
10    fn set_result<T>(&mut self, result: T)
11    where
12        T: Into<RustType>;
13
14    /// Sets the `result`.
15    fn with_result<T>(mut self, result: T) -> Self
16    where
17        T: Into<RustType>,
18    {
19        self.set_result(result);
20        self
21    }
22
23    /// Writes the optional result. (includes the ` -> ` if the result is not `None`)
24    fn write_result(&self, b: &mut CodeBuffer) {
25        if let Some(result) = self.result() {
26            b.write(" -> ");
27            result.write(b);
28        }
29    }
30}