use datafrost::*;
use std::ops::*;
pub struct NumberArray;
pub struct NumberArrayDescriptor {
pub len: usize,
}
impl Kind for NumberArray {
type FormatDescriptor = NumberArrayDescriptor;
type UsageDescriptor = Range<usize>;
}
pub struct PrimaryArray(Vec<u32>);
impl Format for PrimaryArray {
type Kind = NumberArray;
fn allocate(descriptor: &NumberArrayDescriptor) -> Self {
Self(vec![0; descriptor.len])
}
}
pub struct DoubledArray(Vec<u32>);
impl Format for DoubledArray {
type Kind = NumberArray;
fn allocate(descriptor: &NumberArrayDescriptor) -> Self {
Self(vec![0; descriptor.len])
}
}
pub struct DoublePrimaryArray;
impl DerivedDescriptor<PrimaryArray> for DoublePrimaryArray {
type Format = DoubledArray;
fn update(&self, data: &mut DoubledArray, parent: &PrimaryArray, usages: &[&Range<usize>]) {
for range in usages.iter().copied() {
for i in range.clone() {
data.0[i] = 2 * parent.0[i];
}
}
}
}
fn main() {
let ctx = DataFrostContext::new(ContextDescriptor {
label: Some("my context"),
});
let data = ctx.allocate::<PrimaryArray>(AllocationDescriptor {
descriptor: NumberArrayDescriptor { len: 7 },
label: Some("my data"),
derived_formats: &[Derived::new(DoublePrimaryArray)],
});
let mut command_buffer = CommandBuffer::new(CommandBufferDescriptor {
label: Some("my command buffer"),
});
let view = data.view::<PrimaryArray>();
let view_clone = view.clone();
command_buffer.schedule(CommandDescriptor {
label: Some("fill array"),
views: &[&view.as_mut(4..6)],
command: move |ctx| ctx.get_mut(&view_clone).0[4..6].fill(33),
});
let derived = command_buffer.map(&data.view::<DoubledArray>().as_const());
ctx.submit(command_buffer);
assert_eq!(&[0, 0, 0, 0, 66, 66, 0], &ctx.get(&derived).0[..]);
}