Struct Bundle

pub struct Bundle { /* private fields */ }

Implementations§

§

impl Bundle

pub fn insert(&mut self, path: &str, bytes: &[u8]) -> Result<(), AssetError>

Insert or replace content at a canonical path. Identical content keeps the existing payload Arc without allocating a replacement.

use ferravex_engine::assets::Bundle;
let mut bundle = Bundle::default();
bundle.insert("meta/version", b"v1")?;
let reader = bundle.get("meta/version").unwrap();
bundle.insert("meta/version", b"v1")?;
assert!(std::sync::Arc::ptr_eq(&reader, &bundle.get("meta/version").unwrap()));

pub fn len(&self) -> usize

Number of assets, including empty files.

pub fn is_empty(&self) -> bool

pub fn decoded_bytes(&self) -> usize

Total decoded payload bytes, excluding names and externally retained readers.

pub fn remove(&mut self, path: &str) -> Option<Arc<[u8]>>

Remove an asset. Existing Arc readers remain valid.

pub fn apply_edits<'a>( &mut self, edits: impl IntoIterator<Item = (&'a str, Option<&'a [u8]>)>, ) -> Result<(), AssetError>

Apply puts (Some(bytes)) and removals (None) in order, committing only if every path and intermediate capacity is valid. Payloads are Arc-shared. Assets with unchanged final content retain their original payload Arcs, including removal/reinsertion or edits reverted within this transaction.

pub fn apply_edits_checked<'a>( &mut self, edits: impl IntoIterator<Item = (&'a str, Option<&'a [u8]>)>, ) -> Result<(), AssetEditError>

Apply a transaction using normalize_path, reporting the first failing edit’s index, original path and cause. Limits apply after each edit, just as with Self::apply_edits. Aliases of a path are applied in input order; removing an absent asset is a successful no-op. Assets with unchanged final content retain their original payload Arcs.

use ferravex_engine::assets::Bundle;
let mut bundle = Bundle::default();
bundle.apply_edits_checked([(r".\meta\version", Some(&b"v1"[..]))])?;
let before = bundle.encode();
let error = bundle.apply_edits_checked([
    ("meta/version", None), ("../escape", None),
]).unwrap_err();
assert_eq!(error.index, 1);
assert_eq!(bundle.encode(), before);

pub fn subtree(&self, directory: &str) -> Result<Bundle, AssetError>

Copy a directory’s contents with the directory prefix stripped. Empty prefix selects the complete bundle. Payload allocations are shared.

pub fn diff(&self, next: &Bundle) -> BundleDiff

Compare decoded content, ignoring Arc identity and encoding choices.

pub fn get(&self, path: &str) -> Option<Arc<[u8]>>

pub fn get_checked(&self, path: &str) -> Result<Arc<[u8]>, AssetLookupError>

Retrieve shared bytes using a normalized authoring path. Distinguishes invalid paths from missing assets. The reader survives edits and reloads. Successful canonical lookups allocate no memory and copy no payloads.

use ferravex_engine::assets::{AssetLookupError, Bundle};
let mut bundle = Bundle::default();
bundle.insert("meta/version", b"v1")?;
let reader = bundle.get_checked(r".\meta\version")?;
bundle.remove("meta/version");
assert_eq!(&*reader, b"v1");
assert!(matches!(bundle.get_checked("meta/version"),
    Err(AssetLookupError::Missing { .. })));

pub fn get_text(&self, path: &str) -> Result<&str, AssetLookupError>

Borrow validated UTF-8 text. Invalid text reports its canonical path and std::str::Utf8Error, including the first invalid byte’s offset.

use ferravex_engine::assets::Bundle;
let mut bundle = Bundle::default();
bundle.insert("dialogue/greeting", "Hello, 世界!".as_bytes())?;
assert_eq!(bundle.get_text("./dialogue//greeting")?, "Hello, 世界!");

pub fn get_array<const N: usize>( &self, path: &str, ) -> Result<&[u8; N], AssetLookupError>

Borrow an exact-size byte array, reporting both expected and actual lengths on mismatch. Numeric formats should decode explicit endianness; this does not reinterpret memory or impose an implicit wire format.

use ferravex_engine::assets::Bundle;
let mut bundle = Bundle::default();
bundle.insert("level/seed", &123_u32.to_le_bytes())?;
let seed = u32::from_le_bytes(*bundle.get_array::<4>("level/seed")?);
assert_eq!(seed, 123);

pub fn names(&self) -> impl Iterator<Item = &str>

pub fn manifest( &self, ) -> impl ExactSizeIterator + DoubleEndedIterator + FusedIterator

Iterate read-only metadata in ascending canonical path order.

Paths borrow this bundle and are already normalized; checked edits may accept authoring aliases, but the manifest contains only canonical names. No allocation, payload copy or Arc clone is needed. Fingerprints are computed lazily over each yielded asset’s decoded bytes, so a complete traversal scans at most 64 MiB across at most 4096 entries. Results depend only on paths and content, not insertion order or encoding choices.

Retain LiveBundle::current to inspect one generation across reloads. Collect entries if repeated scans are needed; their paths still borrow that retained bundle. This does not add a serialized manifest format or change FXPK0001 encoding. See AssetManifestEntry::fingerprint for the fixed algorithm and collision limitations.

use ferravex_engine::assets::Bundle;
let mut bundle = Bundle::default();
bundle.apply_edits_checked([
    (r".\textures\\flat", Some(&[7; 16][..])),
    ("./meta//empty", Some(&b""[..])),
])?;
let entries: Vec<_> = bundle.manifest().collect();
assert_eq!(entries.iter().map(|entry| entry.path).collect::<Vec<_>>(),
    ["meta/empty", "textures/flat"]);
assert_eq!(entries.iter().map(|entry| entry.byte_len).sum::<usize>(),
    bundle.decoded_bytes());
let decoded = Bundle::decode(&bundle.encode())?;
assert_eq!(entries, decoded.manifest().collect::<Vec<_>>());

pub fn encode(&self) -> Vec<u8> ⓘ

pub fn decode(data: &[u8]) -> Result<Bundle, AssetError>

Trait Implementations§

§

impl Clone for Bundle

§

fn clone(&self) -> Bundle

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
§

impl Default for Bundle

§

fn default() -> Bundle

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Bundle

§

impl RefUnwindSafe for Bundle

§

impl Send for Bundle

§

impl Sync for Bundle

§

impl Unpin for Bundle

§

impl UnwindSafe for Bundle

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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

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.