logo
pub struct FactoryVec<Data> where
    Data: FactoryPrototype
{ /* private fields */ }
Expand description

A container similar to Vec that implements Factory.

Implementations

Create a new FactoryVec.

Examples found in repository
relm4-examples/examples/to_do.rs (line 131)
129
130
131
132
133
134
135
fn main() {
    let model = AppModel {
        tasks: FactoryVec::new(),
    };
    let relm = RelmApp::new(model);
    relm.run();
}
More examples
relm4-examples/examples/grid_factory.rs (line 161)
159
160
161
162
163
164
165
166
fn main() {
    let model = AppModel {
        data: FactoryVec::new(),
        counter: 0,
    };
    let relm = RelmApp::new(model);
    relm.run();
}
relm4-examples/examples/factory_manual.rs (line 118)
116
117
118
119
120
121
122
123
124
fn main() {
    let model = AppModel {
        counters: FactoryVec::new(),
        created_counters: 0,
    };

    let relm = RelmApp::new(model);
    relm.run();
}
relm4-examples/examples/stack_factory.rs (line 123)
121
122
123
124
125
126
127
128
129
fn main() {
    let model = AppModel {
        counters: FactoryVec::new(),
        created_counters: 0,
    };

    let relm = RelmApp::new(model);
    relm.run();
}
relm4-examples/examples/factory.rs (line 104)
102
103
104
105
106
107
108
109
110
fn main() {
    let model = AppModel {
        counters: FactoryVec::new(),
        created_counters: 0,
    };

    let relm = RelmApp::new(model);
    relm.run();
}
relm4-examples/examples/entry_tracker.rs (line 137)
135
136
137
138
139
140
141
142
143
144
145
fn main() {
    let model = AppModel {
        counters: FactoryVec::new(),
        created_counters: 0,
        entry: String::new(),
        tracker: 0,
    };

    let relm = RelmApp::new(model);
    relm.run();
}

Initialize a new FactoryVec with a normal Vec.

Examples found in repository
relm4-examples/libadwaita/examples/sudoku_solver.rs (line 29)
27
28
29
30
31
    fn new() -> Self {
        Self {
            values: FactoryVec::from_vec(vec![Number { value: 0 }; 81]),
        }
    }

Get a slice of the internal data of a FactoryVec.

Get the internal data of the FactoryVec.

Remove all data from the FactoryVec.

Returns the length as amount of elements stored in this type.

Examples found in repository
relm4-examples/libadwaita/examples/advent_calendar.rs (line 140)
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Update => {
                // Check all entries
                for i in 0..self.calendar_entries.len() {
                    // If counter > 1, count down
                    let needs_update = self.calendar_entries.get(i).unwrap().time_left != 0;

                    if needs_update {
                        let entry = self.calendar_entries.get_mut(i).unwrap();
                        entry.time_left = entry.time_left.saturating_sub(1);
                    }
                }
            }
        }
        true
    }
More examples
relm4-examples/libadwaita/examples/sudoku_solver.rs (line 76)
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Solve => {
                let content = self.to_line_string();
                let sudoku = Sudoku::from_str_line(content.as_str()).unwrap();

                if let Some(solution) = sudoku.solve_one() {
                    self.from_string(solution.to_string());
                }
            }
            AppMsg::Clear => {
                for index in 0..self.values.len() {
                    if let Some(v) = self.values.get_mut(index) {
                        v.value = 0;
                    }
                }
            }
            AppMsg::Update(index, value) => {
                if let Some(v) = self.values.get_mut(index) {
                    v.value = value;
                }
            }
        }
        true
    }

Returns true if the length of this type is 0.

Insert an element at the end of a FactoryVec.

Examples found in repository
relm4-examples/examples/to_do.rs (lines 85-88)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::SetCompleted((index, completed)) => {
                if let Some(task) = self.tasks.get_mut(index) {
                    task.completed = completed;
                }
            }
            AppMsg::AddEntry(name) => {
                self.tasks.push(Task {
                    name,
                    completed: false,
                });
            }
        }
        true
    }
More examples
relm4-examples/examples/grid_factory.rs (lines 94-96)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.data.push(Data {
                    counter: self.counter,
                });
                self.counter += 1;
            }
            AppMsg::Remove => {
                self.data.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(data) = self.data.get_mut(index) {
                    data.counter = data.counter.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/factory_manual.rs (lines 32-34)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.counters.push(Counter {
                    value: self.created_counters,
                });
                self.created_counters += 1;
            }
            AppMsg::Remove => {
                self.counters.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/stack_factory.rs (lines 32-34)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.counters.push(Counter {
                    value: self.created_counters,
                });
                self.created_counters += 1;
            }
            AppMsg::Remove => {
                self.counters.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/factory.rs (lines 32-34)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.counters.push(Counter {
                    value: self.created_counters,
                });
                self.created_counters += 1;
            }
            AppMsg::Remove => {
                self.counters.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/entry.rs (lines 40-42)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::AddCounters => {
                let text = self.entry.text();
                if let Ok(v) = text.parse::<i32>() {
                    if v.is_positive() {
                        // add as many counters as user entered
                        for _ in 0..v {
                            self.counters.push(Counter {
                                value: self.created_counters,
                            });
                            self.created_counters += 1;
                        }
                    } else if v.is_negative() {
                        // remove counters
                        for _ in v..0 {
                            self.counters.pop();
                        }
                    }

                    // clearing the entry value clears the entry widget
                    self.entry.set_text("");
                }
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }

Remove an element at the end of a FactoryVec.

Examples found in repository
relm4-examples/examples/grid_factory.rs (line 100)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.data.push(Data {
                    counter: self.counter,
                });
                self.counter += 1;
            }
            AppMsg::Remove => {
                self.data.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(data) = self.data.get_mut(index) {
                    data.counter = data.counter.wrapping_sub(1);
                }
            }
        }
        true
    }
More examples
relm4-examples/examples/factory_manual.rs (line 38)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.counters.push(Counter {
                    value: self.created_counters,
                });
                self.created_counters += 1;
            }
            AppMsg::Remove => {
                self.counters.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/stack_factory.rs (line 38)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.counters.push(Counter {
                    value: self.created_counters,
                });
                self.created_counters += 1;
            }
            AppMsg::Remove => {
                self.counters.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/factory.rs (line 38)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.counters.push(Counter {
                    value: self.created_counters,
                });
                self.created_counters += 1;
            }
            AppMsg::Remove => {
                self.counters.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/entry.rs (line 48)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::AddCounters => {
                let text = self.entry.text();
                if let Ok(v) = text.parse::<i32>() {
                    if v.is_positive() {
                        // add as many counters as user entered
                        for _ in 0..v {
                            self.counters.push(Counter {
                                value: self.created_counters,
                            });
                            self.created_counters += 1;
                        }
                    } else if v.is_negative() {
                        // remove counters
                        for _ in v..0 {
                            self.counters.pop();
                        }
                    }

                    // clearing the entry value clears the entry widget
                    self.entry.set_text("");
                }
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/entry_tracker.rs (line 54)
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        self.reset();

        match msg {
            AppMsg::Modify(value) => {
                self.entry = value;
                if let Ok(v) = self.entry.parse::<i32>() {
                    if v.is_positive() {
                        // add as many counters as user entered
                        for _ in 0..v {
                            self.counters.push(Counter {
                                value: self.created_counters,
                            });
                            self.created_counters += 1;
                        }
                    } else if v.is_negative() {
                        // remove counters
                        for _ in v..0 {
                            self.counters.pop();
                        }
                    }

                    // clearing the entry value clears the entry widget,
                    // as it is tracked in view! if empty
                    self.entry.clear();
                }
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }

Get a reference to data stored at index.

Examples found in repository
relm4-examples/libadwaita/examples/advent_calendar.rs (line 142)
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Update => {
                // Check all entries
                for i in 0..self.calendar_entries.len() {
                    // If counter > 1, count down
                    let needs_update = self.calendar_entries.get(i).unwrap().time_left != 0;

                    if needs_update {
                        let entry = self.calendar_entries.get_mut(i).unwrap();
                        entry.time_left = entry.time_left.saturating_sub(1);
                    }
                }
            }
        }
        true
    }

Get a mutable reference to data stored at index.

Assumes that the data will be modified and the corresponding widget needs to be updated.

Examples found in repository
relm4-examples/libadwaita/examples/sudoku_solver.rs (line 50)
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    fn from_string(&mut self, input: String) {
        for (i, v) in input.chars().enumerate() {
            if v != '.' {
                let value = v.to_string().parse().unwrap();
                if let Some(v) = self.values.get_mut(i) {
                    v.value = value;
                }
            }
        }
    }
}

impl Model for AppModel {
    type Msg = AppMsg;
    type Widgets = AppWidgets;
    type Components = ();
}

impl AppUpdate for AppModel {
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Solve => {
                let content = self.to_line_string();
                let sudoku = Sudoku::from_str_line(content.as_str()).unwrap();

                if let Some(solution) = sudoku.solve_one() {
                    self.from_string(solution.to_string());
                }
            }
            AppMsg::Clear => {
                for index in 0..self.values.len() {
                    if let Some(v) = self.values.get_mut(index) {
                        v.value = 0;
                    }
                }
            }
            AppMsg::Update(index, value) => {
                if let Some(v) = self.values.get_mut(index) {
                    v.value = value;
                }
            }
        }
        true
    }
More examples
relm4-examples/examples/to_do.rs (line 80)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::SetCompleted((index, completed)) => {
                if let Some(task) = self.tasks.get_mut(index) {
                    task.completed = completed;
                }
            }
            AppMsg::AddEntry(name) => {
                self.tasks.push(Task {
                    name,
                    completed: false,
                });
            }
        }
        true
    }
relm4-examples/examples/grid_factory.rs (line 103)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.data.push(Data {
                    counter: self.counter,
                });
                self.counter += 1;
            }
            AppMsg::Remove => {
                self.data.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(data) = self.data.get_mut(index) {
                    data.counter = data.counter.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/factory_manual.rs (line 41)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.counters.push(Counter {
                    value: self.created_counters,
                });
                self.created_counters += 1;
            }
            AppMsg::Remove => {
                self.counters.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/stack_factory.rs (line 41)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.counters.push(Counter {
                    value: self.created_counters,
                });
                self.created_counters += 1;
            }
            AppMsg::Remove => {
                self.counters.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }
relm4-examples/examples/factory.rs (line 41)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Add => {
                self.counters.push(Counter {
                    value: self.created_counters,
                });
                self.created_counters += 1;
            }
            AppMsg::Remove => {
                self.counters.pop();
            }
            AppMsg::Clicked(index) => {
                if let Some(counter) = self.counters.get_mut(index) {
                    counter.value = counter.value.wrapping_sub(1);
                }
            }
        }
        true
    }

Get an immutable iterator for this type

Examples found in repository
relm4-examples/libadwaita/examples/sudoku_solver.rs (line 35)
33
34
35
36
37
38
39
40
41
42
43
44
    fn to_line_string(&self) -> String {
        let mut res = String::new();
        for v in self.values.iter() {
            let string = match v.value {
                0 => String::from("."),
                v @ 1..=9 => format!("{v}"),
                v => panic!("{v} is not defined"),
            };
            res.push_str(string.as_str())
        }
        res
    }

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Key that provides additional information for the FactoryPrototype functions.

Efficiently update the view according to data changes.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.