Skip to content

Commit

Permalink
refactor(codegen): shorten CodeBuffer::take_source_text
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Oct 13, 2024
1 parent 0208706 commit dcec8b5
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions crates/oxc_codegen/src/code_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::mem;

/// A string builder for constructing source code.
///
///
Expand Down Expand Up @@ -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)) }
}
}
}
Expand Down

0 comments on commit dcec8b5

Please sign in to comment.