Skip to content

Commit 6801924

Browse files
committed
into_string()
1 parent 7913b5d commit 6801924

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

turbopack/crates/turbo-tasks-fs/src/rope.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use serde_bytes::ByteBuf;
1818
use tokio::io::{AsyncRead, ReadBuf};
1919
use triomphe::Arc;
2020
use turbo_tasks_hash::{DeterministicHash, DeterministicHasher};
21-
use unsize::{CoerceUnsize, Coercion};
2221

2322
static EMPTY_BUF: &[u8] = &[];
2423

@@ -42,7 +41,7 @@ pub struct Rope {
4241
/// An Arc container for ropes. This indirection allows for easily sharing the
4342
/// contents between Ropes (and also RopeBuilders/RopeReaders).
4443
#[derive(Clone, Debug)]
45-
struct InnerRope(Arc<[RopeElem]>);
44+
struct InnerRope(Arc<Vec<RopeElem>>);
4645

4746
/// Differentiates the types of stored bytes in a rope.
4847
#[derive(Clone, Debug)]
@@ -111,6 +110,10 @@ impl Rope {
111110
pub fn to_bytes(&self) -> Result<Cow<'_, [u8]>> {
112111
self.data.to_bytes(self.length)
113112
}
113+
114+
pub fn into_bytes(self) -> Result<Cow<'static, [u8]>> {
115+
self.data.into_bytes(self.length)
116+
}
114117
}
115118

116119
impl From<Vec<u8>> for Rope {
@@ -139,7 +142,7 @@ impl From<Cow<'static, [u8]>> for Rope {
139142
} else {
140143
Rope {
141144
length: bytes.len(),
142-
data: InnerRope(Arc::from([Local(bytes)]).unsize(Coercion::to_slice())),
145+
data: InnerRope(Arc::new(vec![Local(bytes)])),
143146
}
144147
}
145148
}
@@ -494,11 +497,30 @@ impl InnerRope {
494497
}
495498
}
496499
}
500+
501+
fn into_bytes(mut self, len: usize) -> Result<Cow<'static, [u8]>> {
502+
if self.0.is_empty() {
503+
return Ok(Cow::Borrowed(EMPTY_BUF));
504+
} else if self.0.len() == 1 {
505+
let data = Arc::try_unwrap(self.0);
506+
match data {
507+
Ok(data) => return data.into_iter().next().unwrap().into_bytes(len),
508+
Err(data) => {
509+
self.0 = data;
510+
}
511+
}
512+
}
513+
514+
let mut read = RopeReader::new(&self, 0);
515+
let mut buf = Vec::with_capacity(len);
516+
read.read_to_end(&mut buf)?;
517+
Ok(Cow::Owned(buf))
518+
}
497519
}
498520

499521
impl Default for InnerRope {
500522
fn default() -> Self {
501-
InnerRope(Arc::new([]).unsize(Coercion::to_slice()))
523+
InnerRope(Arc::new(vec![]))
502524
}
503525
}
504526

@@ -537,7 +559,7 @@ impl From<Vec<RopeElem>> for InnerRope {
537559
}
538560

539561
impl Deref for InnerRope {
540-
type Target = Arc<[RopeElem]>;
562+
type Target = Arc<Vec<RopeElem>>;
541563

542564
fn deref(&self) -> &Self::Target {
543565
&self.0
@@ -568,6 +590,13 @@ impl RopeElem {
568590
_ => None,
569591
}
570592
}
593+
594+
fn into_bytes(self, len: usize) -> Result<Cow<'static, [u8]>> {
595+
match self {
596+
Local(bytes) => Ok(bytes),
597+
Shared(inner) => inner.into_bytes(len),
598+
}
599+
}
571600
}
572601

573602
impl DeterministicHash for RopeElem {

turbopack/crates/turbopack-core/src/code_builder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ impl Code {
3434
pub fn has_source_map(&self) -> bool {
3535
!self.mappings.is_empty()
3636
}
37+
38+
/// Take the source code out of the Code.
39+
pub fn into_source_code(self) -> Rope {
40+
self.code
41+
}
3742
}
3843

3944
/// CodeBuilder provides a mutable container to append source code.

0 commit comments

Comments
 (0)