From 64c8e662a0b6e4ad3b44ad4288349c208a25cd1c Mon Sep 17 00:00:00 2001 From: Elias Holzmann <9659253+EliasHolzmann@users.noreply.github.com> Date: Mon, 27 Nov 2023 03:31:29 +0100 Subject: [PATCH] Formatter::with_options: Use different lifetimes Formatter::with_options takes self as a mutable reference (`&'a mut Formatter<'b>`). `'a` and `'b` need to be different lifetimes. Just taking `&'a mut Formatter<'a>` and trusting in Rust being able to implicitely convert from `&'a mut Formatter<'b>` if necessary (after all, `'a` must be smaller than `'b` anyway) fails because `'b` is behind a *mutable* reference. For background on on this behavior, see https://doc.rust-lang.org/nomicon/subtyping.html#variance. --- core/src/fmt/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/fmt/mod.rs b/core/src/fmt/mod.rs index 8e08b7fe983df..a5c0e1ce4e30d 100644 --- a/core/src/fmt/mod.rs +++ b/core/src/fmt/mod.rs @@ -530,7 +530,7 @@ impl<'a> Formatter<'a> { /// Creates a new formatter based on this one with given [`FormattingOptions`]. #[unstable(feature = "formatting_options", issue = "118117")] - pub fn with_options(&'a mut self, options: FormattingOptions) -> Self { + pub fn with_options<'b>(&'b mut self, options: FormattingOptions) -> Formatter<'b> { Formatter { options, buf: self.buf } } }