code_gen/rust/var/
reference.rs1use crate::{CodeBuffer, Expression};
2
3#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default)]
8pub struct Reference {
9 mutable: bool,
10 lifetime: Option<Option<char>>,
11}
12
13impl Reference {
14 pub fn with_mut(self) -> Self {
18 Self {
19 mutable: true,
20 lifetime: self.lifetime,
21 }
22 }
23
24 pub fn with_lifetime(self, c: char) -> Option<Self> {
26 if !c.is_ascii_lowercase() {
27 None
28 } else {
29 Some(Self {
30 mutable: self.mutable,
31 lifetime: Some(Some(c)),
32 })
33 }
34 }
35
36 pub fn with_static_lifetime(self) -> Self {
38 Self {
39 mutable: self.mutable,
40 lifetime: Some(None),
41 }
42 }
43}
44
45impl Expression for Reference {
46 fn write(&self, b: &mut CodeBuffer) {
47 b.write("&");
48 if let Some(lifetime) = self.lifetime {
49 b.write("'");
50 if let Some(lifetime) = lifetime {
51 let mut buffer: [u8; 4] = [0u8; 4];
52 let char_str: &str = lifetime.encode_utf8(&mut buffer);
53 b.write(char_str);
54 b.write(" ");
55 } else {
56 b.write("static ")
57 }
58 }
59 if self.mutable {
60 b.write("mut ");
61 }
62 }
63}