Skip to main content

concinnity_asset/
locator.rs

1// Where an asset's compiled payload lives in the data blob.
2//
3// Carried on a blob-backed asset as a `#[serde(skip)]` field (filled in at load
4// time, not authored) and in the blob defs table's `BlobAssetDef`.
5
6/// Points to an asset's compiled binary payload within the data blob files.
7#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
8pub struct PayloadLocator {
9    /// Index into the blob file list (0 = data/0, 1 = data/1, ...).
10    pub blob_index: u32,
11    /// Byte offset into the payload section of the target blob.
12    pub offset: u64,
13    /// Byte length of the payload.
14    pub len: u64,
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20
21    #[test]
22    fn a_locator_round_trips_through_the_blob_defs_table() {
23        // The defs table is postcard, so the offset and length have to survive a
24        // format that carries no field names and varint-encodes every number.
25        let loc = PayloadLocator {
26            blob_index: 2,
27            offset: 4_294_967_296,
28            len: 1_048_576,
29        };
30        let bytes = postcard::to_allocvec(&loc).unwrap();
31        assert_eq!(postcard::from_bytes::<PayloadLocator>(&bytes).unwrap(), loc);
32    }
33
34    #[test]
35    fn a_locator_parses_from_its_json_form() {
36        let loc: PayloadLocator =
37            serde_json::from_str(r#"{"blob_index":1,"offset":64,"len":256}"#).unwrap();
38        assert_eq!(loc.blob_index, 1);
39        assert_eq!(loc.offset, 64);
40        assert_eq!(loc.len, 256);
41        assert_ne!(
42            loc,
43            PayloadLocator {
44                blob_index: 0,
45                offset: 64,
46                len: 256,
47            }
48        );
49        assert_eq!(loc.clone(), loc);
50        assert!(alloc::format!("{loc:?}").contains("PayloadLocator"));
51    }
52}