Skip to main content

ZstdAsset

Struct ZstdAsset 

Source
pub struct ZstdAsset { /* private fields */ }
Expand description

编译期压缩的 zstd 资源

Implementations§

Source§

impl ZstdAsset

Source

pub fn metadata(&self) -> &ZstdMetadata

返回原始文件的元数据信息

Examples found in repository?
examples/example_usage.rs (line 13)
7fn main() {
8    // 使用 include_zstd! 宏加载文件
9    // 路径相对于此示例文件所在的目录 (examples/)
10    let asset = include_zstd::include_zstd!("sample.txt");
11
12    // 获取文件元数据
13    let metadata = asset.metadata();
14    println!("文件大小: {} 字节", metadata.len);
15    println!("是否为文件: {}", metadata.is_file);
16    println!("是否为目录: {}", metadata.is_dir);
17
18    if let Some(modified) = metadata.modified {
19        println!("修改时间: {:?}", modified);
20    }
21
22    // 获取解压后的文件内容
23    let bytes = asset.bytes();
24    println!("解压后内容长度: {} 字节", bytes.len());
25    println!(
26        "内容预览:\n{}",
27        String::from_utf8_lossy(&bytes[..bytes.len().min(200)])
28    );
29
30    // 验证缓存机制:多次调用 bytes() 返回相同的内存地址
31    let ptr1 = asset.bytes().as_ptr();
32    let ptr2 = asset.bytes().as_ptr();
33    assert_eq!(ptr1, ptr2, "缓存机制应该返回相同的指针");
34    println!("\n缓存验证通过:两次调用 bytes() 返回相同指针");
35}
Source

pub fn bytes(&self) -> &[u8]

返回 zstd 解压后的文件内容(惰性解压,首次调用时解压并缓存)

Examples found in repository?
examples/example_usage.rs (line 23)
7fn main() {
8    // 使用 include_zstd! 宏加载文件
9    // 路径相对于此示例文件所在的目录 (examples/)
10    let asset = include_zstd::include_zstd!("sample.txt");
11
12    // 获取文件元数据
13    let metadata = asset.metadata();
14    println!("文件大小: {} 字节", metadata.len);
15    println!("是否为文件: {}", metadata.is_file);
16    println!("是否为目录: {}", metadata.is_dir);
17
18    if let Some(modified) = metadata.modified {
19        println!("修改时间: {:?}", modified);
20    }
21
22    // 获取解压后的文件内容
23    let bytes = asset.bytes();
24    println!("解压后内容长度: {} 字节", bytes.len());
25    println!(
26        "内容预览:\n{}",
27        String::from_utf8_lossy(&bytes[..bytes.len().min(200)])
28    );
29
30    // 验证缓存机制:多次调用 bytes() 返回相同的内存地址
31    let ptr1 = asset.bytes().as_ptr();
32    let ptr2 = asset.bytes().as_ptr();
33    assert_eq!(ptr1, ptr2, "缓存机制应该返回相同的指针");
34    println!("\n缓存验证通过:两次调用 bytes() 返回相同指针");
35}

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.