jsonapi_rs/
array.rs

1//! Defines trait and implementations that allow a `has many` relationship to be optional
2use crate::model::JsonApiModel;
3
4/// Trait which allows a `has many` relationship to be optional.
5pub trait JsonApiArray<M> {
6    fn get_models(&self) -> &[M];
7    fn get_models_mut(&mut self) -> &mut [M];
8}
9
10impl<M: JsonApiModel> JsonApiArray<M> for Vec<M> {
11    fn get_models(&self) -> &[M] { self }
12    fn get_models_mut(&mut self) -> &mut [M] { self }
13}
14
15impl<M: JsonApiModel> JsonApiArray<M> for Option<Vec<M>> {
16    fn get_models(&self) -> &[M] {
17        self.as_ref()
18            .map(|v| v.as_slice())
19            .unwrap_or(&[][..])
20    }
21
22    fn get_models_mut(&mut self) -> &mut [M] {
23        self.as_mut()
24            .map(|v| v.as_mut_slice())
25            .unwrap_or(&mut [][..])
26    }
27}