Skip to content

Commit

Permalink
VecModel<T>: Don't require that T implements Default
Browse files Browse the repository at this point in the history
ChangeLog: Rust: Removed the requirement that for VecModel<T> T has to implement Default.
  • Loading branch information
tronical committed Feb 4, 2025
1 parent 0267733 commit 9961a6f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion internal/core/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,17 @@ impl<M: Model> Model for Rc<M> {
}

/// A [`Model`] backed by a `Vec<T>`, using interior mutability.
#[derive(Default)]
pub struct VecModel<T> {
array: RefCell<Vec<T>>,
notify: ModelNotify,
}

impl<T> Default for VecModel<T> {
fn default() -> Self {
Self { array: Default::default(), notify: Default::default() }
}
}

impl<T: 'static> VecModel<T> {
/// Allocate a new model from a slice
pub fn from_slice(slice: &[T]) -> ModelRc<T>
Expand Down Expand Up @@ -1540,4 +1545,15 @@ mod tests {
assert_eq!(model.iter().max().unwrap(), 9);
assert_eq!(model.max_requested_row.get(), 9);
}

#[test]
fn vecmodel_doesnt_require_default() {
#[derive(Clone)]
struct MyNoDefaultType {
_foo: bool,
}
let model = VecModel::<MyNoDefaultType>::default();
assert_eq!(model.row_count(), 0);
model.push(MyNoDefaultType { _foo: true });
}
}

0 comments on commit 9961a6f

Please sign in to comment.