Skip to content

Commit 1004eb2

Browse files
authored
refactor: RawBufferSource (#173)
1 parent abb8ead commit 1004eb2

File tree

3 files changed

+23
-84
lines changed

3 files changed

+23
-84
lines changed

Cargo.lock

Lines changed: 0 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ itertools = "0.13"
4242
codspeed-criterion-compat = { version = "2.7.2", default-features = false, optional = true }
4343
static_assertions = "1.1.0"
4444
simd-json = "0.14.3"
45-
ouroboros = "0.18.5"
4645

4746
[dev-dependencies]
4847
twox-hash = "2.1.0"

src/raw_source.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use std::{
44
sync::OnceLock,
55
};
66

7-
use ouroboros::self_referencing;
8-
97
use crate::{
108
helpers::{
119
get_generated_source_info, stream_chunks_of_raw_source, OnChunk, OnName,
@@ -364,59 +362,57 @@ impl StreamChunks for RawStringSource {
364362
/// assert_eq!(s.map(&MapOptions::default()), None);
365363
/// assert_eq!(s.size(), 16);
366364
/// ```
367-
#[self_referencing]
368365
pub struct RawBufferSource {
369366
value: Vec<u8>,
370-
#[borrows(value)]
371-
#[not_covariant]
372-
value_as_string: OnceLock<Cow<'this, str>>,
367+
value_as_string: OnceLock<Option<String>>,
373368
}
374369

375370
impl RawBufferSource {
371+
#[allow(unsafe_code)]
376372
fn get_or_init_value_as_string(&self) -> &str {
377-
self.with(|fields| {
378-
fields
379-
.value_as_string
380-
.get_or_init(|| String::from_utf8_lossy(fields.value))
381-
})
373+
self
374+
.value_as_string
375+
.get_or_init(|| match String::from_utf8_lossy(&self.value) {
376+
Cow::Owned(s) => Some(s),
377+
Cow::Borrowed(_) => None,
378+
})
379+
.as_deref()
380+
.unwrap_or_else(|| unsafe { std::str::from_utf8_unchecked(&self.value) })
382381
}
383382
}
384383

385384
impl Clone for RawBufferSource {
386385
fn clone(&self) -> Self {
387-
RawBufferSourceBuilder {
388-
value: self.borrow_value().clone(),
389-
value_as_string_builder: |_: &Vec<u8>| Default::default(),
386+
Self {
387+
value: self.value.clone(),
388+
value_as_string: Default::default(),
390389
}
391-
.build()
392390
}
393391
}
394392

395393
impl PartialEq for RawBufferSource {
396394
fn eq(&self, other: &Self) -> bool {
397-
self.borrow_value() == other.borrow_value()
395+
self.value == other.value
398396
}
399397
}
400398

401399
impl Eq for RawBufferSource {}
402400

403401
impl From<Vec<u8>> for RawBufferSource {
404402
fn from(value: Vec<u8>) -> Self {
405-
RawBufferSourceBuilder {
403+
Self {
406404
value,
407-
value_as_string_builder: |_: &Vec<u8>| Default::default(),
405+
value_as_string: Default::default(),
408406
}
409-
.build()
410407
}
411408
}
412409

413410
impl From<&[u8]> for RawBufferSource {
414411
fn from(value: &[u8]) -> Self {
415-
RawBufferSourceBuilder {
412+
Self {
416413
value: value.to_vec(),
417-
value_as_string_builder: |_: &Vec<u8>| Default::default(),
414+
value_as_string: Default::default(),
418415
}
419-
.build()
420416
}
421417
}
422418

@@ -430,19 +426,19 @@ impl Source for RawBufferSource {
430426
}
431427

432428
fn buffer(&self) -> Cow<[u8]> {
433-
Cow::Borrowed(self.borrow_value())
429+
Cow::Borrowed(&self.value)
434430
}
435431

436432
fn size(&self) -> usize {
437-
self.borrow_value().len()
433+
self.value.len()
438434
}
439435

440436
fn map(&self, _: &MapOptions) -> Option<SourceMap> {
441437
None
442438
}
443439

444440
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
445-
writer.write_all(self.borrow_value())
441+
writer.write_all(&self.value)
446442
}
447443
}
448444

@@ -456,7 +452,7 @@ impl std::fmt::Debug for RawBufferSource {
456452
write!(
457453
f,
458454
"{indent_str}RawBufferSource::from({:?}).boxed()",
459-
self.borrow_value()
455+
&self.value
460456
)
461457
}
462458
}
@@ -499,7 +495,7 @@ mod tests {
499495
// Fix https://github.com/web-infra-dev/rspack/issues/6793
500496
#[test]
501497
fn fix_rspack_issue_6793() {
502-
let source1 = RawSource::from("hello\n\n".to_string());
498+
let source1 = RawStringSource::from_static("hello\n\n");
503499
let source1 = ReplaceSource::new(source1);
504500
let source2 = OriginalSource::new("world".to_string(), "world.txt");
505501
let concat = ConcatSource::new([source1.boxed(), source2.boxed()]);

0 commit comments

Comments
 (0)