From dcec8b585055d28db53fd00570403b7128463ea5 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Sun, 13 Oct 2024 10:33:14 +0100 Subject: [PATCH] refactor(codegen): shorten `CodeBuffer::take_source_text` --- crates/oxc_codegen/src/code_buffer.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/oxc_codegen/src/code_buffer.rs b/crates/oxc_codegen/src/code_buffer.rs index cf071acf1266a2..9c6461d6ce9a7b 100644 --- a/crates/oxc_codegen/src/code_buffer.rs +++ b/crates/oxc_codegen/src/code_buffer.rs @@ -1,3 +1,5 @@ +use std::mem; + /// A string builder for constructing source code. /// /// @@ -331,16 +333,11 @@ impl CodeBuffer { /// ``` #[must_use] pub fn take_source_text(&mut self) -> String { - use std::mem::take; - - #[cfg(debug_assertions)] - { - String::from_utf8(take(&mut self.buf)).unwrap() - } - #[cfg(not(debug_assertions))] - { + if cfg!(debug_assertions) { + String::from_utf8(mem::take(&mut self.buf)).unwrap() + } else { // SAFETY: All methods of `CodeBuffer` ensure `buf` is valid UTF-8 - unsafe { String::from_utf8_unchecked(take(&mut self.buf)) } + unsafe { String::from_utf8_unchecked(mem::take(&mut self.buf)) } } } }