I handle logging errors for my axum server when I implement IntoResponse for my custom error types. It works great and it's really clean but now I have some situations where I don't have errors in response to web requests for things like internal jobs. I still would like to use a similar system where I do the logging in the implementation code but implement it for () so it does nothing past that. Is this possible?
You cannot implement
impl axum::response::IntoResponse for ()
Only the axum
(where IntoResponse
is local) or std
(where ()
is local) can do that.
And as it turns out, axum
has already done that.
You can't override that implementation; if you want to do something other than what they do, make your own unit struct and implement it for that instead.
Perhaps I wasn't clear, I have no intent to do anything with Axum, I was using that as an example of the kind of behavior I wanted to emulate with my own type and ().
I'm still not entirely sure what your question is then. But if you have your own trait, you can implement that trait for ()
.
Implementing your own trait for ()
can look like this, for example:
pub trait MyTrait {
fn name(&self) -> &'static str;
fn print_hello(&self) {
println!("Hello!");
}
}
impl MyTrait for () {
fn name(&self) -> &'static str {
"unit"
}
}
impl MyTrait for i32 {
fn name(&self) -> &'static str {
"integer(32)"
}
fn print_hello(&self) {
println!("Number {} says hello.", self);
}
}
pub fn some_func<T: MyTrait>(arg: T) {
println!("Called some_func with arg.name() = {}.", arg.name());
arg.print_hello();
}
fn main() {
some_func(());
some_func(17);
println!("------");
().print_hello();
3.print_hello();
}
Output:
Called some_func with arg.name() = unit.
Hello!
Called some_func with arg.name() = integer(32).
Number 17 says hello.
------
Hello!
Number 3 says hello.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.