# resource/blobasset (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/resource/blobasset/

Binary asset. (the original resource/blobasset.js)
- The original holds the Blob received with fetch. The Rust version holds the bytes as is.

## BlobAsset (struct) (Default, Asset)

Binary asset.
- Holds the received bytes as is without decoding. (asset type blob)

Example: Read with bytes in hand and take them out

```rust
use vanilla::{Asset, BlobAsset};

let mut blob_asset = BlobAsset::new();
assert!(blob_asset.get_blob().is_none());
blob_asset.load_from_bytes("data.bin", vec![1, 2, 3]);
assert!(blob_asset.is_loaded());
assert_eq!(blob_asset.get_blob().expect("bytes").as_slice(), &[1, 2, 3]);
blob_asset.unload();
assert!(!blob_asset.is_loaded());
```

- `pub fn new() -> BlobAsset` (constructor, since 0.3.2): Constructor.
  - returns BlobAsset: Empty binary asset before a request.
- `pub fn load(&mut self, engine: &Engine, asset_path: &str) -> AssetLoadHandle` (method, since 0.3.2): Requests a read. (the original load, returns a load handle that can be awaited)
  - If already requested, does not request again and returns the first handle.
  - `engine` &Engine: Engine to ask for the bytes.
  - `asset_path` &str: Asset path. (relative to the asset folder on desktop, to asset_base_url on the web; data: URLs also work)
  - returns AssetLoadHandle: Load handle. (on desktop it is read on the spot and already finished. Finishes in failure if it cannot be read)

  Example: Read a data: URL on the spot on desktop

  ```rust
  use vanilla::{BlobAsset, Engine, EngineConfiguration};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut blob_asset = BlobAsset::new();
  let load_handle = blob_asset.load(&engine, "data:,abc");
  assert!(load_handle.is_loaded());
  assert_eq!(blob_asset.get_blob().expect("bytes").as_slice(), b"abc");
  ```

- `pub fn load_from_bytes(&mut self, asset_path: &str, bytes: Vec<u8>)` (method, since 0.3.2): Reads from bytes already in hand.
  - Replaces it with a new handle even if a requested handle exists.
  - `asset_path` &str: Path to record.
  - `bytes` Vec<u8>: Asset bytes.
- `pub fn unload(&mut self)` (method, since 0.3.2): Unloads.
- `pub fn get_blob(&self) -> Option<Rc<Vec<u8>>>` (method, since 0.3.2): Bytes. (the original getBlob. None before it is fully read)
  - returns Option<Rc<Vec<u8>>>: Loaded bytes. None before a request, while loading, or on failure.
