pub struct PathCache<K>{ /* private fields */ }
Expand description
A simple cache for path data.
Implementations§
Source§impl<K> PathCache<K>
impl<K> PathCache<K>
Sourcepub fn get_cached_path(&self, path_key: &K) -> Option<&[Position]>
pub fn get_cached_path(&self, path_key: &K) -> Option<&[Position]>
Returns the cached path, if it exists. Returns None if the path isn’t cached.
§Examples
use screeps::{Direction, Position, RoomCoordinate};
use screeps_pathfinding::utils::cache::PathCache;
// Create a new path cache that uses String as the key type
let mut cache: PathCache<String> = PathCache::new();
// Build a path
let start = Position::new(
RoomCoordinate::try_from(24).unwrap(),
RoomCoordinate::try_from(18).unwrap(),
"E5N6".parse().unwrap(),
);
let goal = start.checked_add_direction(Direction::Right).unwrap();
let path = vec!(start, goal);
let existing_key = "existing_key".to_string();
// Actually store the path in the cache
assert_eq!(cache.is_path_cached(&existing_key), false);
cache.update_cached_path(&existing_key, path.into_iter());
assert_eq!(cache.is_path_cached(&existing_key), true);
// Pull the cached copy of the path
let path_opt = cache.get_cached_path(&existing_key);
assert!(path_opt.is_some());
let path_slice = path_opt.unwrap();
assert!(path_slice.len() == 2);
assert!(path_slice[0] == start);
assert!(path_slice[1] == goal);
// Attempt to pull a non-existent path entry
let nonexisting_key = "nonexisting_key".to_string();
let path_opt = cache.get_cached_path(&nonexisting_key);
assert!(path_opt.is_none());
assert_eq!(cache.is_path_cached(&nonexisting_key), false);
Sourcepub fn is_path_cached(&self, path_key: &K) -> bool
pub fn is_path_cached(&self, path_key: &K) -> bool
Returns whether a path is cached with the provided path key.
§Examples
use screeps::{Direction, Position, RoomCoordinate};
use screeps_pathfinding::utils::cache::PathCache;
// Create a new path cache that uses String as the key type
let mut cache: PathCache<String> = PathCache::new();
// A path doesn't exist yet in the cache for this key
let key = "some_key".to_string();
assert_eq!(cache.is_path_cached(&key), false);
// Build a path
let start = Position::new(
RoomCoordinate::try_from(24).unwrap(),
RoomCoordinate::try_from(18).unwrap(),
"E5N6".parse().unwrap(),
);
let goal = start.checked_add_direction(Direction::Right).unwrap();
let path = vec!(start, goal);
// Store the path in the cache
cache.update_cached_path(&key, path.into_iter());
assert_eq!(cache.is_path_cached(&key), true);
Sourcepub fn get_path<G>(
&mut self,
path_key: &K,
generator_fn: G,
) -> Option<&[Position]>
pub fn get_path<G>( &mut self, path_key: &K, generator_fn: G, ) -> Option<&[Position]>
Returns the path, generating and caching it if it’s not already cached.
Returns None only if the path is not cached and the generation function returns None.
§Examples
use screeps::{Direction, Position, RoomCoordinate};
use screeps_pathfinding::utils::cache::PathCache;
// Create a new path cache that uses String as the key type
let mut cache: PathCache<String> = PathCache::new();
// The path doesn't exist yet in the cache
let key = "some_key".to_string();
assert_eq!(cache.is_path_cached(&key), false);
let start = Position::new(
RoomCoordinate::try_from(24).unwrap(),
RoomCoordinate::try_from(18).unwrap(),
"E5N6".parse().unwrap(),
);
let goal = start.checked_add_direction(Direction::Right).unwrap();
// Pull the path, generating it since it doesn't exist
let path_opt = cache.get_path(&key, |path_key| {
// Build a path
let path = vec!(start, goal);
Some(path)
});
assert!(path_opt.is_some());
let path_slice = path_opt.unwrap();
assert!(path_slice.len() == 2);
assert!(path_slice[0] == start);
assert!(path_slice[1] == goal);
assert_eq!(cache.is_path_cached(&key), true);
Sourcepub fn update_cached_path(
&mut self,
path_key: &K,
path_iter: impl Iterator<Item = Position>,
)
pub fn update_cached_path( &mut self, path_key: &K, path_iter: impl Iterator<Item = Position>, )
Updates the path cache for a specific key.
This allows for pre-loading the cache with any existing path data you might already have available.
§Examples
use screeps::{Direction, Position, RoomCoordinate};
use screeps_pathfinding::utils::cache::PathCache;
// Create a new path cache that uses String as the key type
let mut cache: PathCache<String> = PathCache::new();
// Build a path
let start = Position::new(
RoomCoordinate::try_from(24).unwrap(),
RoomCoordinate::try_from(18).unwrap(),
"E5N6".parse().unwrap(),
);
let goal = start.checked_add_direction(Direction::Right).unwrap();
let path = vec!(start, goal);
let existing_key = "existing_key".to_string();
// Store the path in the cache
cache.update_cached_path(&existing_key, path.into_iter());
// Pull the cached copy of the path
let path_opt = cache.get_cached_path(&existing_key);
assert!(path_opt.is_some());
let path_slice = path_opt.unwrap();
assert!(path_slice.len() == 2);
assert!(path_slice[0] == start);
assert!(path_slice[1] == goal);
Sourcepub fn remove_cached_path(&mut self, path_key: &K)
pub fn remove_cached_path(&mut self, path_key: &K)
Removes the path cached for a specific key.
Trait Implementations§
Source§impl<'de, K> Deserialize<'de> for PathCache<K>
impl<'de, K> Deserialize<'de> for PathCache<K>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl<K> Freeze for PathCache<K>
impl<K> RefUnwindSafe for PathCache<K>where
K: RefUnwindSafe,
impl<K> Send for PathCache<K>where
K: Send,
impl<K> Sync for PathCache<K>where
K: Sync,
impl<K> Unpin for PathCache<K>where
K: Unpin,
impl<K> UnwindSafe for PathCache<K>where
K: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more