mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-14 22:56:44 +02:00
Remove generics from logger exercise (#1899)
The logger exercise comes before the section on generics, and the purpose of the exercise is for students to get practice writing a trait implementation, so using generics in the solution is a source of confusion for students. I've removed the generic and made `VerbosityFilter` directly hold a `StderrLogger`.
This commit is contained in:
parent
9059a1aa38
commit
025fbffa99
@ -2,7 +2,7 @@
|
|||||||
minutes: 20
|
minutes: 20
|
||||||
---
|
---
|
||||||
|
|
||||||
# Exercise: Generic Logger
|
# Exercise: Logger Trait
|
||||||
|
|
||||||
Let's design a simple logging utility, using a trait `Logger` with a `log`
|
Let's design a simple logging utility, using a trait `Logger` with a `log`
|
||||||
method. Code which might log its progress can then take an `&impl Logger`. In
|
method. Code which might log its progress can then take an `&impl Logger`. In
|
||||||
|
@ -36,12 +36,12 @@ fn do_things(logger: &impl Logger) {
|
|||||||
// ANCHOR_END: setup
|
// ANCHOR_END: setup
|
||||||
|
|
||||||
/// Only log messages up to the given verbosity level.
|
/// Only log messages up to the given verbosity level.
|
||||||
struct VerbosityFilter<L: Logger> {
|
struct VerbosityFilter {
|
||||||
max_verbosity: u8,
|
max_verbosity: u8,
|
||||||
inner: L,
|
inner: StderrLogger,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<L: Logger> Logger for VerbosityFilter<L> {
|
impl Logger for VerbosityFilter {
|
||||||
fn log(&self, verbosity: u8, message: impl Display) {
|
fn log(&self, verbosity: u8, message: impl Display) {
|
||||||
if verbosity <= self.max_verbosity {
|
if verbosity <= self.max_verbosity {
|
||||||
self.inner.log(verbosity, message);
|
self.inner.log(verbosity, message);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user