From 992c1c7a6dc58efa550ceabe85cb766fdbb0d22e Mon Sep 17 00:00:00 2001 From: parrrate Date: Thu, 9 Apr 2026 12:56:16 +0000 Subject: [PATCH] update all_errors solution --- src/exercises/all_errors.md | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/exercises/all_errors.md b/src/exercises/all_errors.md index 723a00b..ed8899b 100644 --- a/src/exercises/all_errors.md +++ b/src/exercises/all_errors.md @@ -4,30 +4,39 @@ Make this compile and pass tests: ```rust # trait Collects { -# type Error; +# type Fast; +# type Slow; # type Output; -# fn push(&self, trace: Vec, next: i32) -> Result, Self::Error>; -# fn regularize(&self, result: Result, Vec>, Self::Error>) -> Result, Self::Output>; +# fn create(&self, next: i32) -> Result; +# fn update(&self, slow: &mut Self::Slow, next: i32); +# fn regularize(&self, result: Result, Self::Slow>, Self::Fast>) -> Result, Self::Output>; # } # struct First; # struct All; # impl Collects for First { -# type Error = i32; +# type Fast = i32; +# type Slow = std::convert::Infallible; # type Output = i32; -# fn push(&self, _trace: Vec, next: i32) -> Result, Self::Error> { Err(next) } -# fn regularize(&self, result: Result, Vec>, Self::Error>) -> Result, Self::Output> { result.map(|r| r.unwrap()) } +# fn create(&self, next: i32) -> Result { Err(next) } +# fn update(&self, slow: &mut Self::Slow, _next: i32) { match *slow {} } +# fn regularize(&self, result: Result, Self::Slow>, Self::Fast>) -> Result, Self::Output> { result.map(|r| r.unwrap_or_else(|slow| match slow {} )) } # } # impl Collects for All { -# type Error = std::convert::Infallible; +# type Fast = std::convert::Infallible; +# type Slow = Vec; # type Output = Vec; -# fn push(&self, mut trace: Vec, next: i32) -> Result, Self::Error> { trace.push(next); Ok(trace) } -# fn regularize(&self, result: Result, Vec>, Self::Error>) -> Result, Self::Output> { result.unwrap() } +# fn create(&self, next: i32) -> Result { let mut slow = vec![]; slow.push(next); Ok(slow) } +# fn update(&self, slow: &mut Self::Slow, next: i32) { slow.push(next) } +# fn regularize(&self, result: Result, Self::Slow>, Self::Fast>) -> Result, Self::Output> { result.unwrap_or_else(|fast| match fast {}) } # } # fn first() -> impl Collects { First } # fn all() -> impl Collects> { All } -# fn _only_postivie(numbers: Vec, c: &C) -> Result, Vec>, C::Error> { +# fn _only_postivie(numbers: Vec, c: &C) -> Result, C::Slow>, C::Fast> { # let mut state = Ok(vec![]); -# for x in numbers { state = if x > 0 { state.map(|mut vec| {vec.push(x); vec}) } else { Err(c.push(match state { Ok(_) => vec![], Err(vec) => vec }, x)?) } } +# for x in numbers { state = if x > 0 { state.map(|mut vec| {vec.push(x); vec}) } else { Err(match state { +# Ok(_) => c.create(x)?, +# Err(mut slow) => { c.update(&mut slow, x) ; slow } +# }) } } # Ok(state) # } # fn only_positive(numbers: Vec, c: C) -> Result, C::Output> { c.regularize(_only_postivie(numbers, &c)) }