Skip to content
This is the development version of the documentation. It may change before the next release. See 0.6.x for the latest release.

DatagramBuilder

DatagramBuilder is a struct that accumulates the commands to send and converts them into Frames. It is obtained via Client::datagram_builder.

push accumulates commands, and build converts them into Frames. The resulting Frames is split into multiple Frames; send them one frame at a time with Client::send / send_checked.

let mut builder = client.datagram_builder();
builder.push(SetSilencer::default());
let frames = builder.build()?;
for frame in &frames {
client.send_checked(frame).await?;
}
Method Description
push(cmd) Accumulate a command common to all devices
push_each(assign) Accumulate a different command per device (see below)
build() Convert the accumulated commands into Frames

push_each takes a closure that receives a device and assigns a different command per device. In Rust it is FnMut(&Device) -> Option<C>, in Python Callable[[Device], Command | None], and in C# Func<Device, ICommand?>. For a device where None is returned, nothing is assigned in that step.

let mut builder = client.datagram_builder();
builder.push_each(|device| {
Some(if device.idx() % 2 == 0 {
Pattern::new(&left)
} else {
Pattern::new(&right)
})
});
let frames = builder.build()?;

In Rust, the return values of the closure must all be the same type. To mix commands of different types, either unify them into a BoxedCommand with Command::boxed, or call push_each once per type. Because Python is dynamically typed, commands of different types can be returned per device as-is.

let mut builder = client.datagram_builder();
builder.push_each(|device| {
Some(if device.idx() % 2 == 0 {
Pattern::new(&left).boxed()
} else {
Modulation::new(SamplingConfig::FREQ_4K, &modulation).boxed()
})
});
let frames = builder.build()?;