Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/cached_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,33 +116,43 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
}
}

impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks
impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks<'_>
for CachedSource<T>
{
fn stream_chunks(
&self,
fn stream_chunks<'a>(
&'a self,
options: &MapOptions,
on_chunk: crate::helpers::OnChunk,
on_source: crate::helpers::OnSource,
on_name: crate::helpers::OnName,
on_chunk: crate::helpers::OnChunk<'_, 'a>,
on_source: crate::helpers::OnSource<'_, 'a>,
on_name: crate::helpers::OnName<'_, 'a>,
) -> crate::helpers::GeneratedInfo {
let cached_map = self.cached_maps.entry(options.clone());
match cached_map {
Entry::Occupied(entry) => {
let source = self.source();
let source = self
.cached_source
.get_or_init(|| self.inner.source().into());
if let Some(map) = entry.get() {
#[allow(unsafe_code)]
// SAFETY: We guarantee that once a `SourceMap` is stored in the cache, it will never be removed.
// Therefore, even if we force its lifetime to be longer, the reference remains valid.
// This is based on the following assumptions:
// 1. `SourceMap` will be valid for the entire duration of the application.
// 2. The cached `SourceMap` will not be manually removed or replaced, ensuring the reference's safety.
let map =
unsafe { std::mem::transmute::<&SourceMap, &'a SourceMap>(map) };
stream_chunks_of_source_map(
&source, map, on_chunk, on_source, on_name, options,
source, map, on_chunk, on_source, on_name, options,
)
} else {
stream_chunks_of_raw_source(
&source, options, on_chunk, on_source, on_name,
source, options, on_chunk, on_source, on_name,
)
}
}
Entry::Vacant(entry) => {
let (generated_info, map) = stream_and_get_source_and_map(
self.original(),
&self.inner as &T,
options,
on_chunk,
on_source,
Expand Down
24 changes: 12 additions & 12 deletions src/concat_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,22 @@ impl PartialEq for ConcatSource {
}
impl Eq for ConcatSource {}

impl StreamChunks for ConcatSource {
impl<'a> StreamChunks<'a> for ConcatSource {
fn stream_chunks(
&self,
&'a self,
options: &MapOptions,
on_chunk: OnChunk,
on_source: OnSource,
on_name: OnName,
on_chunk: OnChunk<'_, 'a>,
on_source: OnSource<'_, 'a>,
on_name: OnName<'_, 'a>,
) -> crate::helpers::GeneratedInfo {
if self.children().len() == 1 {
return self.children()[0]
return self.children[0]
.stream_chunks(options, on_chunk, on_source, on_name);
}
let mut current_line_offset = 0;
let mut current_column_offset = 0;
let mut source_mapping: HashMap<String, u32> = HashMap::default();
let mut name_mapping: HashMap<String, u32> = HashMap::default();
let mut source_mapping: HashMap<Cow<str>, u32> = HashMap::default();
let mut name_mapping: HashMap<Cow<str>, u32> = HashMap::default();
let mut need_to_close_mapping = false;

let source_index_mapping: RefCell<HashMap<u32, u32>> =
Expand Down Expand Up @@ -262,10 +262,10 @@ impl StreamChunks for ConcatSource {
}
},
&mut |i, source, source_content| {
let mut global_index = source_mapping.get(source).copied();
let mut global_index = source_mapping.get(&source).copied();
if global_index.is_none() {
let len = source_mapping.len() as u32;
source_mapping.insert(source.to_owned(), len);
source_mapping.insert(source.clone(), len);
on_source(len, source, source_content);
global_index = Some(len);
}
Expand All @@ -274,10 +274,10 @@ impl StreamChunks for ConcatSource {
.insert(i, global_index.unwrap());
},
&mut |i, name| {
let mut global_index = name_mapping.get(name).copied();
let mut global_index = name_mapping.get(&name).copied();
if global_index.is_none() {
let len = name_mapping.len() as u32;
name_mapping.insert(name.to_owned(), len);
name_mapping.insert(name.clone(), len);
on_name(len, name);
global_index = Some(len);
}
Expand Down
Loading
Loading