1
Fork 0

Combine held types

This commit is contained in:
Andy Killorin 2023-12-26 15:52:13 -06:00
parent 7cb74f83f8
commit a7377c5855
Signed by: ank
GPG key ID: B6241CA3B552BCA4

View file

@ -1,7 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use tracing::{warn, info, trace}; use tracing::{warn, info, trace};
use tokio::sync::{Mutex, OwnedMutexGuard, Semaphore}; use tokio::sync::{Mutex, OwnedMutexGuard, Semaphore, OwnedSemaphorePermit};
use crate::{blocks::Position, turtle::TurtleCommander}; use crate::{blocks::Position, turtle::TurtleCommander};
use crate::turtle::{TurtleCommand::*, TurtleCommandResponse}; use crate::turtle::{TurtleCommand::*, TurtleCommandResponse};
@ -17,21 +17,39 @@ pub struct Depots {
depot_semaphore: Arc<Semaphore>, depot_semaphore: Arc<Semaphore>,
} }
pub struct DepotGuard {
mutex: OwnedMutexGuard<Position>,
#[allow(unused)]
semaphore: OwnedSemaphorePermit, // "dropped in declaration order"
// - reference chapter 10.8
}
impl DepotGuard {
pub fn new(mutex: OwnedMutexGuard<Position>, semaphore: OwnedSemaphorePermit) -> Self { Self { mutex, semaphore } }
fn position(&self) -> &Position {
&self.mutex
}
}
impl Depots { impl Depots {
/// Nearest depot to the given position /// Nearest depot to the given position
pub async fn nearest(&self, pos: Position) -> Option<OwnedMutexGuard<Position>> { pub async fn nearest(&self, pos: Position) -> DepotGuard {
self.depots.lock().await let permit = self.depot_semaphore.clone().acquire_owned().await.unwrap();
let mutex = self.depots.lock().await
.iter().map(|i| i.clone()) .iter().map(|i| i.clone())
.filter_map(|i| i.try_lock_owned().ok()) .filter_map(|i| i.try_lock_owned().ok())
.min_by_key(|d| d.manhattan(pos)) .min_by_key(|d| d.manhattan(pos))
.map(|d| d) .map(|d| d);
DepotGuard::new(mutex.unwrap(), permit)
} }
pub async fn dock(&self, turtle: TurtleCommander) -> Option<usize> { pub async fn dock(&self, turtle: TurtleCommander) -> Option<usize> {
let permit = self.depot_semaphore.clone().acquire_owned().await.unwrap(); let depot = self.clone().nearest(turtle.pos().await).await;
let depot = self.clone().nearest(turtle.pos().await).await?; trace!("depot at {:?}", depot.position());
trace!("depot at {depot:?}"); turtle.goto(*depot.position()).await?;
turtle.goto(*depot).await?;
// dump inventory // dump inventory
for i in 1..=16 { for i in 1..=16 {
@ -61,7 +79,6 @@ impl Depots {
turtle.execute(Backward(4)).await; turtle.execute(Backward(4)).await;
drop(depot); drop(depot);
drop(permit);
// lava bucket fix // lava bucket fix
for i in 1..=16 { for i in 1..=16 {