Skip to content

Commit 0b41044

Browse files
committed
update: QuestionModelContainer removed to favour immutable keys
1 parent 7c08794 commit 0b41044

File tree

7 files changed

+112
-116
lines changed

7 files changed

+112
-116
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ All notable changes to this project will be documented in this file.
2828

2929
- Neetcode 75 question list.
3030

31-
### Fixed
31+
### Changed
3232

3333
- Not null constraints on the fields that are never null from the server.
34+
- `QuestionModelContainer { question: RefCell<QuestionModel> }` changed to `Rc<RefCell<QuestionModel>>`
35+
- As prior implemented hash. Hashables should not be mutable.
3436

3537
## [0.2.0] - 2023-07-30
3638

src/app_ui/components/list.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use ratatui::widgets::ListState;
2-
use std::rc::Rc;
32

43
#[derive(Debug, Clone)]
54
pub struct StatefulList<T> {
65
pub state: ListState,
7-
pub items: Vec<Rc<T>>,
6+
pub items: Vec<T>,
87
}
98

109
impl<T> Default for StatefulList<T> {
@@ -21,10 +20,10 @@ impl<T> StatefulList<T> {
2120
if self.items.is_empty() {
2221
self.state.select(Some(0))
2322
}
24-
self.items.push(Rc::new(item))
23+
self.items.push(item)
2524
}
2625

27-
pub fn get_selected_item(&self) -> Option<&Rc<T>> {
26+
pub fn get_selected_item(&self) -> Option<&T> {
2827
match self.state.selected() {
2928
Some(i) => Some(&self.items[i]),
3029
None => None,
@@ -38,7 +37,7 @@ impl<T> StatefulList<T> {
3837
}
3938
StatefulList {
4039
state: list_state,
41-
items: items.into_iter().map(|item| Rc::new(item)).collect(),
40+
items,
4241
}
4342
}
4443

src/app_ui/components/popups/selection_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Component for SelectionListPopup {
6060
.items
6161
.iter()
6262
.map(|item| {
63-
let line_text = item.as_ref();
63+
let line_text = item;
6464
ListItem::new(Span::styled(line_text, Style::default()))
6565
})
6666
.collect::<Vec<_>>();

src/app_ui/helpers/question.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
use std::{cell::RefCell, rc::Rc};
22

33
use crate::entities::QuestionModel;
4-
use std::hash::Hash;
5-
6-
#[derive(PartialEq, Eq, Debug, Ord, PartialOrd)]
7-
pub struct QuestionModelContainer {
8-
pub question: RefCell<QuestionModel>,
9-
}
10-
11-
// RefCell keys are mutable and should not be used in types where hashing
12-
// is required. This implementation is valid until question_frontend_id change.
13-
// For more refer https://rust-lang.github.io/rust-clippy/master/index.html#/mutable_key_type
14-
impl Hash for QuestionModelContainer {
15-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
16-
self.question.borrow().hash(state)
17-
}
18-
}
194

205
pub struct Stats<'a> {
21-
pub qm: &'a Vec<Rc<QuestionModelContainer>>,
6+
pub qm: &'a Vec<Rc<RefCell<QuestionModel>>>,
227
}
238

249
impl<'a> Stats<'a> {
@@ -66,8 +51,8 @@ impl<'a> Stats<'a> {
6651
self.qm
6752
.iter()
6853
.filter(|q| {
69-
if let Some(st) = &q.question.borrow().status {
70-
st.as_str() == status && difficulty == q.question.borrow().difficulty.as_str()
54+
if let Some(st) = &q.borrow().status {
55+
st.as_str() == status && difficulty == q.borrow().difficulty.as_str()
7156
} else {
7257
false
7358
}
@@ -79,7 +64,7 @@ impl<'a> Stats<'a> {
7964
self.qm
8065
.iter()
8166
.filter(|q| {
82-
if let Some(st) = &q.question.borrow().status {
67+
if let Some(st) = &q.borrow().status {
8368
st.as_str() == status
8469
} else {
8570
false
@@ -91,7 +76,7 @@ impl<'a> Stats<'a> {
9176
fn get_diff_count(&self, difficulty: &str) -> usize {
9277
self.qm
9378
.iter()
94-
.filter(|q| q.question.borrow().difficulty.as_str() == difficulty)
79+
.filter(|q| q.borrow().difficulty.as_str() == difficulty)
9580
.count()
9681
}
9782
}

src/app_ui/widgets/notification.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
use std::rc::Rc;
1+
use std::{cell::RefCell, rc::Rc};
22

3-
use crate::app_ui::helpers::question::QuestionModelContainer;
43
use crate::{
54
app_ui::components::{
65
help_text::HelpText,
76
popups::{paragraph::ParagraphPopup, selection_list::SelectionListPopup},
87
},
9-
entities::TopicTagModel,
8+
entities::{QuestionModel, TopicTagModel},
109
};
1110

1211
#[derive(Debug, Clone)]
@@ -54,7 +53,7 @@ impl<T> NotifContent<T> {
5453
#[derive(Debug, Clone)]
5554
pub enum Notification {
5655
Questions(NotifContent<Vec<TopicTagModel>>),
57-
Stats(NotifContent<Vec<Rc<QuestionModelContainer>>>),
56+
Stats(NotifContent<Vec<Rc<RefCell<QuestionModel>>>>),
5857
Popup(NotifContent<PopupMessage>),
5958
HelpText(NotifContent<IndexSet<HelpText>>),
6059
Event(NotifContent<KeyEvent>),

0 commit comments

Comments
 (0)