'Don't allow user to override value of DerefMut struct, but allow it to execute mutable function on it
I'm currently develloping my own library for vectors and matrices, and to simplify my life, I defined my Matrix to be a Vec of Vector, and defined the Deref trait as such:
pub struct Matrix(Vec<RowVector>);
impl Deref for Matrix {
type Target = [RowVector];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Matrix {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
This work like a charm, but it has one flaw: you can override one row to be a RowVector of a different size of the rest, which is obviously VERY BAD.
Am I doomed? is there a solution to disallow the overwrite but allow to mutate the Vector ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
