//! Defines trait and implementations that allow a `has many` relationship to be optional use crate::model::JsonApiModel; /// Trait which allows a `has many` relationship to be optional. pub trait JsonApiArray { fn get_models(&self) -> &[M]; fn get_models_mut(&mut self) -> &mut [M]; } impl JsonApiArray for Vec { fn get_models(&self) -> &[M] { self } fn get_models_mut(&mut self) -> &mut [M] { self } } impl JsonApiArray for Option> { fn get_models(&self) -> &[M] { self.as_ref() .map(|v| v.as_slice()) .unwrap_or(&[][..]) } fn get_models_mut(&mut self) -> &mut [M] { self.as_mut() .map(|v| v.as_mut_slice()) .unwrap_or(&mut [][..]) } }