pub struct CachedSource<T> { /* private fields */ }
Expand description

It tries to reused cached results from other methods to avoid calculations, usually used after modify is finished.

use rspack_sources::{
  BoxSource, CachedSource, ConcatSource, MapOptions, OriginalSource,
  RawSource, Source, SourceExt, SourceMap,
};

let mut concat = ConcatSource::new([
  RawSource::from("Hello World\n".to_string()).boxed(),
  OriginalSource::new(
    "console.log('test');\nconsole.log('test2');\n",
    "console.js",
  )
  .boxed(),
]);
concat.add(OriginalSource::new("Hello2\n", "hello.md"));

let cached = CachedSource::new(concat);

assert_eq!(
  cached.source(),
  "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
);
// second time will be fast.
assert_eq!(
  cached.source(),
  "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
);

Implementations§

source§

impl<T> CachedSource<T>

source

pub fn new(inner: T) -> Self

Create a CachedSource with the original Source.

source

pub fn original(&self) -> &T

Get the original Source.

Trait Implementations§

source§

impl<T: Source> Clone for CachedSource<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for CachedSource<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<T: Hash> Hash for CachedSource<T>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T: PartialEq> PartialEq for CachedSource<T>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T>

source§

fn source(&self) -> Cow<'_, str>

Get the source code.
source§

fn buffer(&self) -> Cow<'_, [u8]>

Get the source buffer.
source§

fn size(&self) -> usize

Get the size of the source.
source§

fn map(&self, options: &MapOptions) -> Option<SourceMap>

Get the SourceMap.
source§

fn to_writer(&self, writer: &mut dyn Write) -> Result<()>

Writes the source into a writer, preferably a std::io::BufWriter<std::io::Write>.
source§

fn update_hash(&self, state: &mut dyn Hasher)

Update hash based on the source.
source§

impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks for CachedSource<T>

source§

fn stream_chunks( &self, options: &MapOptions, on_chunk: OnChunk<'_>, on_source: OnSource<'_>, on_name: OnName<'_> ) -> GeneratedInfo

StreamChunks abstraction
source§

impl<T: Eq> Eq for CachedSource<T>

Auto Trait Implementations§

§

impl<T> Freeze for CachedSource<T>

§

impl<T> !RefUnwindSafe for CachedSource<T>

§

impl<T> Send for CachedSource<T>
where T: Sync + Send,

§

impl<T> Sync for CachedSource<T>
where T: Sync + Send,

§

impl<T> Unpin for CachedSource<T>

§

impl<T> !UnwindSafe for CachedSource<T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> SourceExt for T
where T: Source + 'static,

source§

fn boxed(self) -> Arc<dyn Source>

An alias for BoxSource::from.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.