Skip to content

Commit f8f3c79

Browse files
committed
AssetRef: Add helper to create partial parent from original
1 parent b81bf40 commit f8f3c79

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/asset.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,41 @@ impl AssetRef {
11001100
f(Some(&self), child);
11011101
}
11021102
}
1103+
1104+
/// Get an [`AssetRef`] representing a subset of this parent's children.
1105+
///
1106+
/// # Panics
1107+
///
1108+
/// Panics if this asset is not a parent asset or `num_units` is zero or exceeds the total
1109+
/// capacity of this asset.
1110+
pub fn make_partial_parent(&self, num_units: u32) -> Self {
1111+
assert!(
1112+
self.is_parent(),
1113+
"Cannot make a partial parent from a non-parent asset"
1114+
);
1115+
assert!(
1116+
num_units > 0,
1117+
"Cannot make a partial parent with zero units"
1118+
);
1119+
1120+
let (max_num_units, unit_size) = match self.capacity() {
1121+
AssetCapacity::Discrete(max_num_units, unit_size) => (max_num_units, unit_size),
1122+
// We know asset capacity type is discrete as this is a parent asset
1123+
AssetCapacity::Continuous(_) => unreachable!(),
1124+
};
1125+
match num_units.cmp(&max_num_units) {
1126+
// Make a new Asset with fewer units
1127+
Ordering::Less => Self::from(Asset {
1128+
capacity: Cell::new(AssetCapacity::Discrete(num_units, unit_size)),
1129+
..Asset::clone(self)
1130+
}),
1131+
// Same number of units as self
1132+
Ordering::Equal => self.clone(),
1133+
Ordering::Greater => {
1134+
panic!("Cannot make a partial parent with more units than original")
1135+
}
1136+
}
1137+
}
11031138
}
11041139

11051140
impl From<Rc<Asset>> for AssetRef {

0 commit comments

Comments
 (0)