oxedyne/fe2o3/fe2o3_syntax/src/opt.rs
1.4 KiB, 17 runs
created by r1870400018:1057, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_core::prelude::*; |
| 2 | |
| 3 | pub trait OptionRefVec<T> { |
| 4 | fn with_len<'a>(self, len: usize) -> Outcome<&'a [T]> where Self: 'a; |
| 5 | } |
| 6 | |
| 7 | impl<'a, T> OptionRefVec<T> for Option<&'a Vec<T>> { |
| 8 | fn with_len<'b>(self, len: usize) -> Outcome<&'b [T]> where 'a: 'b { |
| 9 | self.map_or_else( |
| 10 | || Err(err!("Expected {} values, found none.", len; Missing, Data)), |
| 11 | |vals| { |
| 12 | if vals.len() == len { |
| 13 | Ok(vals.as_slice()) |
| 14 | } else { |
| 15 | Err(err!( |
| 16 | "Expected {} values, found {}.", len, vals.len(); |
| 17 | Mismatch, Data, Size)) |
| 18 | } |
| 19 | } |
| 20 | ) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | pub trait OptionMutVec<T> { |
| 25 | fn with_len<'a>(&'a mut self, len: usize) -> Outcome<&'a mut [T]> where Self: 'a; |
| 26 | } |
| 27 | |
| 28 | impl<'a, T> OptionMutVec<T> for Option<&'a mut Vec<T>> { |
| 29 | fn with_len<'b>(&'b mut self, len: usize) -> Outcome<&'b mut [T]> where 'a: 'b { |
| 30 | self.as_deref_mut().map_or_else( |
| 31 | || Err(err!("Expected {} values, found none.", len; Missing, Data)), |
| 32 | |vals| { |
| 33 | let vals = vals.as_mut_slice(); |
| 34 | if vals.len() == len { |
| 35 | Ok(vals) |
| 36 | } else { |
| 37 | Err(err!( |
| 38 | "Expected {} values, found {}.", len, vals.len(); |
| 39 | Mismatch, Data, Size)) |
| 40 | } |
| 41 | } |
| 42 | ) |
| 43 | } |
| 44 | } |