refactor(codegen): shorten CodeBuffer::take_source_text (#6495)

Pure refactor.
This commit is contained in:
overlookmotel 2024-10-13 09:51:47 +00:00
parent 951def6e35
commit 5843e01bed

View file

@ -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)) }
}
}
}