Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor GroupGuard #44

Merged
merged 2 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions autd3/src/controller/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ use crate::prelude::AUTDError;
use super::Controller;
use super::Link;

pub struct GroupGuard<'a, K: PartialEq + Debug, L: Link, F: Fn(&Device) -> Option<K>> {
pub struct GroupGuard<'a, K: PartialEq + Debug, L: Link> {
pub(crate) cnt: &'a mut Controller<L>,
pub(crate) f: F,
pub(crate) keys: Vec<Option<K>>,
pub(crate) timeout: Option<Duration>,
pub(crate) parallel_threshold: Option<usize>,
pub(crate) operations: Vec<(Box<dyn Operation>, Box<dyn Operation>)>,
}

impl<'a, K: PartialEq + Debug, L: Link, F: Fn(&Device) -> Option<K>> GroupGuard<'a, K, L, F> {
pub(crate) fn new(cnt: &'a mut Controller<L>, f: F) -> Self {
impl<'a, K: PartialEq + Debug, L: Link> GroupGuard<'a, K, L> {
pub(crate) fn new(cnt: &'a mut Controller<L>, f: impl Fn(&Device) -> Option<K>) -> Self {
Self {
operations: (0..cnt.geometry.num_devices())
.map(|_| {
Expand All @@ -32,8 +32,8 @@ impl<'a, K: PartialEq + Debug, L: Link, F: Fn(&Device) -> Option<K>> GroupGuard<
)
})
.collect(),
keys: cnt.geometry.devices().map(f).collect(),
cnt,
f,
timeout: None,
parallel_threshold: None,
}
Expand All @@ -46,17 +46,16 @@ impl<'a, K: PartialEq + Debug, L: Link, F: Fn(&Device) -> Option<K>> GroupGuard<
<<D as Datagram>::G as OperationGenerator>::O2: 'static,
{
let Self {
f,
keys,
mut operations,
cnt,
timeout,
parallel_threshold,
} = self;

if !cnt
.geometry
.devices()
.any(|dev| (f)(dev).map(|kk| kk == k).unwrap_or(false))
if !keys
.iter()
.any(|key| key.as_ref().map(|kk| kk == &k).unwrap_or(false))
{
return Err(AUTDInternalError::UnkownKey(format!("{:?}", k)));
}
Expand All @@ -76,10 +75,11 @@ impl<'a, K: PartialEq + Debug, L: Link, F: Fn(&Device) -> Option<K>> GroupGuard<

operations
.iter_mut()
.zip(keys.iter())
.zip(cnt.geometry.devices())
.for_each(|(op, dev)| {
if let Some(kk) = (f)(dev) {
if kk == k {
.for_each(|((op, key), dev)| {
if let Some(kk) = key {
if kk == &k {
tracing::debug!("Generate operation for device {}", dev.idx());
let (op1, op2) = generator.generate(dev);
*op = (Box::new(op1) as Box<_>, Box::new(op2) as Box<_>);
Expand All @@ -89,7 +89,7 @@ impl<'a, K: PartialEq + Debug, L: Link, F: Fn(&Device) -> Option<K>> GroupGuard<

Ok(Self {
cnt,
f,
keys,
timeout,
parallel_threshold,
operations,
Expand Down
2 changes: 1 addition & 1 deletion autd3/src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<L: Link> Controller<L> {
pub fn group<K: Hash + Eq + Clone + Debug, F: Fn(&Device) -> Option<K>>(
&mut self,
f: F,
) -> GroupGuard<K, L, F> {
) -> GroupGuard<K, L> {
GroupGuard::new(self, f)
}
}
Expand Down