Accommodate clippy suggestions.

This commit is contained in:
Stu Black 2025-01-08 15:07:11 -05:00
parent 063fedd709
commit d627b8b964
6 changed files with 68 additions and 102 deletions

View File

@ -1,5 +1,3 @@
use symbol_map;
/// Internal edge identifier.
///
/// This type is not exported by the crate because it does not identify the
@ -23,15 +21,9 @@ impl EdgeId {
/// states. This type is not exported by the crate because it does not identify
/// the graph that it belongs to, which makes it only slightly less dangerous
/// than a pointer with no lifetime.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct VertexId(pub usize);
impl Default for VertexId {
fn default() -> Self {
VertexId(0)
}
}
impl symbol_map::SymbolId for VertexId {
fn next(&self) -> Self {
VertexId(self.0 + 1)

View File

@ -60,6 +60,12 @@ pub struct Graph<T: Hash + Eq + Clone, S, A> {
arcs: Vec<RawEdge<A>>, // Indexed by EdgeId.
}
impl<T: Hash + Eq + Clone, S, A> Default for Graph<T, S, A> {
fn default() -> Self {
Graph::new()
}
}
impl<T: Hash + Eq + Clone, S, A> Graph<T, S, A> {
/// Creates an empty `Graph` with no vertices or edges.
pub fn new() -> Self {
@ -126,20 +132,14 @@ impl<T: Hash + Eq + Clone, S, A> Graph<T, S, A> {
///
/// If `state` does not correspond to a known game state, returns `None`.
pub fn find_node<'s>(&'s self, state: &T) -> Option<nav::Node<'s, T, S, A>> {
match self.state_ids.get(state) {
Some(symbol) => Some(nav::Node::new(self, *symbol.id())),
None => None,
}
self.state_ids.get(state).map(|symbol| nav::Node::new(self, *symbol.id()))
}
/// Gets a mutable node handle for the given game state.
///
/// If `state` does not correspond to a known game state, returns `None`.
pub fn find_node_mut<'s>(&'s mut self, state: &T) -> Option<mutators::MutNode<'s, T, S, A>> {
match self.state_ids.get(state).map(|s| s.id().clone()) {
Some(id) => Some(mutators::MutNode::new(self, id)),
None => None,
}
pub fn find_node_mut(&mut self, state: &T) -> Option<mutators::MutNode<'_, T, S, A>> {
self.state_ids.get(state).map(|s| *s.id()).map(move |id| mutators::MutNode::new(self, id))
}
/// Adds a vertex (with no parents or children) for the given game state and
@ -149,8 +149,8 @@ impl<T: Hash + Eq + Clone, S, A> Graph<T, S, A> {
/// ignoring the `data` parameter. As a result, this method is guaranteed to
/// return a handle for a root vertex only when `state` is a novel game
/// state.
pub fn add_node<'s>(&'s mut self, state: T, data: S) -> mutators::MutNode<'s, T, S, A> {
let node_id = match self.state_ids.get_or_insert(state).map(|s| s.id().clone()) {
pub fn add_node(&mut self, state: T, data: S) -> mutators::MutNode<'_, T, S, A> {
let node_id = match self.state_ids.get_or_insert(state).map(|s| *s.id()) {
Insertion::Present(id) => id,
Insertion::New(id) => {
self.add_raw_vertex(data);
@ -179,7 +179,7 @@ impl<T: Hash + Eq + Clone, S, A> Graph<T, S, A> {
F: for<'b> FnOnce(nav::Node<'b, T, S, A>) -> S,
G: for<'b> FnOnce(nav::Node<'b, T, S, A>) -> S,
{
let source_id = match self.state_ids.get_or_insert(source).map(|s| s.id().clone()) {
let source_id = match self.state_ids.get_or_insert(source).map(|s| *s.id()) {
Insertion::Present(id) => id,
Insertion::New(id) => {
let data = source_data(nav::Node::new(self, id));
@ -187,7 +187,7 @@ impl<T: Hash + Eq + Clone, S, A> Graph<T, S, A> {
id
}
};
let dest_id = match self.state_ids.get_or_insert(dest).map(|s| s.id().clone()) {
let dest_id = match self.state_ids.get_or_insert(dest).map(|s| *s.id()) {
Insertion::Present(id) => id,
Insertion::New(id) => {
let data = dest_data(nav::Node::new(self, id));

View File

@ -34,11 +34,11 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutNode<'a, T, S, A> {
MutNode { graph, id }
}
fn vertex<'s>(&'s self) -> &'s RawVertex<S> {
fn vertex(&self) -> &RawVertex<S> {
self.graph.get_vertex(self.id)
}
fn vertex_mut<'s>(&'s mut self) -> &'s mut RawVertex<S> {
fn vertex_mut(&mut self) -> &mut RawVertex<S> {
self.graph.get_vertex_mut(self.id)
}
@ -54,16 +54,16 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutNode<'a, T, S, A> {
/// consistently return a single value, regardless of which value was used
/// to obtain this node handle.
pub fn get_label(&self) -> &T {
&self.graph.get_state(self.id).unwrap()
self.graph.get_state(self.id).unwrap()
}
/// Returns the data at this vertex.
pub fn get_data<'s>(&'s self) -> &'s S {
pub fn get_data(&self) -> &S {
&self.vertex().data
}
/// Returns the data at this vertex, mutably.
pub fn get_data_mut<'s>(&'s mut self) -> &'s mut S {
pub fn get_data_mut(&mut self) -> &mut S {
&mut self.vertex_mut().data
}
@ -79,13 +79,13 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutNode<'a, T, S, A> {
/// Returns a traversible list of outgoing edges. Its lifetime will be
/// limited to a local borrow of `self`.
pub fn get_child_list<'s>(&'s self) -> ChildList<'s, T, S, A> {
pub fn get_child_list(&self) -> ChildList<'_, T, S, A> {
ChildList::new(self.graph, self.id)
}
/// Returns a traversible list of outgoing edges. Its lifetime will be
/// limited to a local borrow of `self`.
pub fn get_child_list_mut<'s>(&'s mut self) -> MutChildList<'s, T, S, A> {
pub fn get_child_list_mut(&mut self) -> MutChildList<'_, T, S, A> {
MutChildList {
graph: self.graph,
id: self.id,
@ -103,13 +103,13 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutNode<'a, T, S, A> {
/// Returns a traversible list of incoming edges. Its lifetime will be
/// limited to a local borrow of `self`.
pub fn get_parent_list<'s>(&'s self) -> ParentList<'s, T, S, A> {
pub fn get_parent_list(&self) -> ParentList<'_, T, S, A> {
ParentList::new(self.graph, self.id)
}
/// Returns a traversible list of incoming edges. Its lifetime will be
/// limited to a local borrow of `self`.
pub fn get_parent_list_mut<'s>(&'s mut self) -> MutParentList<'s, T, S, A> {
pub fn get_parent_list_mut(&mut self) -> MutParentList<'_, T, S, A> {
MutParentList {
graph: self.graph,
id: self.id,
@ -135,7 +135,7 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutNode<'a, T, S, A> {
/// Returns a non-mutating node obtained by borrowing this node. Returns a
/// value whose lifetime is limited to a borrow of `self`.
pub fn get_node<'s>(&'s self) -> Node<'s, T, S, A> {
pub fn get_node(&self) -> Node<'_, T, S, A> {
Node::new(self.graph, self.id)
}
}
@ -147,7 +147,7 @@ pub struct MutChildList<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> {
}
impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutChildList<'a, T, S, A> {
fn vertex<'s>(&'s self) -> &'s RawVertex<S> {
fn vertex(&self) -> &RawVertex<S> {
self.graph.get_vertex(self.id)
}
@ -162,17 +162,17 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutChildList<'a, T, S, A> {
}
/// Returns an edge handle for the `i`th edge.
pub fn get_edge<'s>(&'s self, i: usize) -> Edge<'s, T, S, A> {
pub fn get_edge(&self, i: usize) -> Edge<'_, T, S, A> {
Edge::new(self.graph, self.vertex().children[i])
}
/// Returns an edge handle for the `i`th edge. Its lifetime will be limited
/// to a local borrow of `self`.
pub fn get_edge_mut<'s>(&'s mut self, i: usize) -> MutEdge<'s, T, S, A> {
pub fn get_edge_mut(&mut self, i: usize) -> MutEdge<'_, T, S, A> {
let id = self.vertex().children[i];
MutEdge {
graph: self.graph,
id: id,
id,
}
}
@ -182,19 +182,19 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutChildList<'a, T, S, A> {
let id = self.vertex().children[i];
MutEdge {
graph: self.graph,
id: id,
id,
}
}
/// Returns a node handle for the vertex these edges originate from. Its
/// lifetime will be limited to a local borrow of `self`.
pub fn get_source_node<'s>(&'s self) -> Node<'s, T, S, A> {
pub fn get_source_node(&self) -> Node<'_, T, S, A> {
Node::new(self.graph, self.id)
}
/// Returns a mutable node handle for the vertex these edges originate
/// from. Its lifetime will be limited to a local borrow of `self`.
pub fn get_source_node_mut<'s>(&'s mut self) -> MutNode<'s, T, S, A> {
pub fn get_source_node_mut(&mut self) -> MutNode<'_, T, S, A> {
MutNode {
graph: self.graph,
id: self.id,
@ -212,7 +212,7 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutChildList<'a, T, S, A> {
}
/// Returns an iterator over child edges.
pub fn iter<'s>(&'s self) -> ChildListIter<'s, T, S, A> {
pub fn iter(&self) -> ChildListIter<'_, T, S, A> {
self.get_source_node().get_child_list().iter()
}
@ -220,9 +220,7 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutChildList<'a, T, S, A> {
/// vertex exists, it is created and associated with the data returned by
/// `f`. Returns a mutable edge handle for the new edge, with a lifetime
/// limited to a borrow of `self`.
pub fn add_child<'s, F>(&'s mut self, child_label: T, f: F, edge_data: A) -> MutEdge<'s, T, S, A>
where
F: FnOnce() -> S,
pub fn add_child(&mut self, child_label: T, f: impl FnOnce() -> S, edge_data: A) -> MutEdge<'_, T, S, A>
{
let target_id = match self
.graph
@ -277,7 +275,7 @@ pub struct MutParentList<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> {
}
impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutParentList<'a, T, S, A> {
fn vertex<'s>(&'s self) -> &'s RawVertex<S> {
fn vertex(&self) -> &RawVertex<S> {
self.graph.get_vertex(self.id)
}
@ -293,13 +291,13 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutParentList<'a, T, S, A> {
/// Returns a node handle for the vertex these edges originate terminate
/// on. Its lifetime will be limited to a local borrow of `self`.
pub fn get_target_node<'s>(&'s self) -> Node<'s, T, S, A> {
pub fn get_target_node(&self) -> Node<'_, T, S, A> {
Node::new(self.graph, self.id)
}
/// Returns a mutable node handle for the vertex these edges terminate
/// on. Its lifetime will be limited to a local borrow of `self`.
pub fn get_target_node_mut<'s>(&'s mut self) -> MutNode<'s, T, S, A> {
pub fn get_target_node_mut(&mut self) -> MutNode<'_, T, S, A> {
MutNode {
graph: self.graph,
id: self.id,
@ -318,17 +316,17 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutParentList<'a, T, S, A> {
/// Returns a handle to the `i`th edge. Its lifetime will be limited to a
/// local borrow of `self`.
pub fn get_edge<'s>(&'s self, i: usize) -> Edge<'s, T, S, A> {
pub fn get_edge(&self, i: usize) -> Edge<'_, T, S, A> {
Edge::new(self.graph, self.vertex().parents[i])
}
/// Returns a mutable handle to the `i`th edge. Its lifetime will be limited
/// to a local borrow of `self`.
pub fn get_edge_mut<'s>(&'s mut self, i: usize) -> MutEdge<'s, T, S, A> {
pub fn get_edge_mut(&mut self, i: usize) -> MutEdge<'_, T, S, A> {
let id = self.vertex().parents[i];
MutEdge {
graph: self.graph,
id: id,
id,
}
}
@ -338,12 +336,12 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutParentList<'a, T, S, A> {
let id = self.vertex().parents[i];
MutEdge {
graph: self.graph,
id: id,
id,
}
}
/// Returns an iterator over parent edges.
pub fn iter<'s>(&'s self) -> ParentListIter<'s, T, S, A> {
pub fn iter(&self) -> ParentListIter<'_, T, S, A> {
self.get_target_node().get_parent_list().iter()
}
@ -351,14 +349,12 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutParentList<'a, T, S, A> {
/// vertex exists, it is created and associated with the data returned by
/// `f`. Returns a mutable edge handle for the new edge, with a lifetime
/// limited to a borrow of `self`.
pub fn add_parent<'s, F>(
&'s mut self,
pub fn add_parent(
&mut self,
parent_label: T,
f: F,
f: impl FnOnce() -> S,
edge_data: A,
) -> MutEdge<'s, T, S, A>
where
F: FnOnce() -> S,
) -> MutEdge<'_, T, S, A>
{
let source_id = match self
.graph
@ -451,17 +447,17 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutEdge<'a, T, S, A> {
/// Returns the target of this edge. Returns a node handle, whose lifetime is
/// limited to a local borrow of `self`.
pub fn get_target<'s>(&'s self) -> Node<'s, T, S, A> {
pub fn get_target(&self) -> Node<'_, T, S, A> {
Node::new(self.graph, self.arc().target)
}
/// Returns the target of this edge. Returns a mutable node handle will be
/// available, whose lifetime is limited to a local borrow of `self`.
pub fn get_target_mut<'s>(&'s mut self) -> MutNode<'s, T, S, A> {
pub fn get_target_mut(&mut self) -> MutNode<'_, T, S, A> {
let id = self.arc().target;
MutNode {
graph: self.graph,
id: id,
id,
}
}
@ -471,23 +467,23 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutEdge<'a, T, S, A> {
let id = self.arc().target;
MutNode {
graph: self.graph,
id: id,
id,
}
}
/// Returns a node handle for the source of this edge. Its lifetime will be
/// limited to a local borrow of `self`.
pub fn get_source<'s>(&'s self) -> Node<'s, T, S, A> {
pub fn get_source(&self) -> Node<'_, T, S, A> {
Node::new(self.graph, self.arc().source)
}
/// Returns a mutable node handle for the source of this edge. Its lifetime
/// will be limited to a local borrow of `self`.
pub fn get_source_mut<'s>(&'s mut self) -> MutNode<'s, T, S, A> {
pub fn get_source_mut(&mut self) -> MutNode<'_, T, S, A> {
let id = self.arc().source;
MutNode {
graph: self.graph,
id: id,
id,
}
}
@ -498,7 +494,7 @@ impl<'a, T: Hash + Eq + Clone + 'a, S: 'a, A: 'a> MutEdge<'a, T, S, A> {
let id = self.arc().source;
MutNode {
graph: self.graph,
id: id,
id,
}
}

View File

@ -51,7 +51,7 @@ where
/// consistently return a single value, regardless of which value was used
/// to obtain this node handle.
pub fn get_label(&self) -> &T {
&self.graph.get_state(self.id).unwrap()
self.graph.get_state(self.id).unwrap()
}
/// Returns an immutable ID that is guaranteed to identify this vertex

View File

@ -18,7 +18,7 @@ use crate::Graph;
/// Errors that may arise during search.
#[derive(Debug)]
pub enum SearchError<E: Error> {
pub enum SearchError<E: Error + 'static> {
/// A search operation selected a child index that was out of bounds.
ChildBounds {
/// The index of the child that was requested.
@ -112,24 +112,10 @@ impl<E: Error> fmt::Display for SearchError<E> {
}
}
impl<E: Error> Error for SearchError<E> {
fn description(&self) -> &str {
match *self {
SearchError::ChildBounds {
requested_index: _,
child_count: _,
} => "child out of bounds",
SearchError::ParentBounds {
requested_index: _,
parent_count: _,
} => "parent out of bounds",
SearchError::SelectionError(ref e) => e.description(),
}
}
fn cause(&self) -> Option<&dyn Error> {
match *self {
SearchError::SelectionError(ref e) => Some(e),
impl<E: Error + 'static> Error for SearchError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
SearchError::SelectionError(e) => Some(e),
_ => None,
}
}
@ -153,7 +139,7 @@ impl<'a, T: 'a + Hash + Eq + Clone, S: 'a, A: 'a> Stack<'a, T, S, A> {
/// Removes the most recently traversed element from the path, if
/// any. Returns a handle for any edge that was removed.
pub fn pop<'s>(&'s mut self) -> Option<Edge<'s, T, S, A>> {
pub fn pop(&mut self) -> Option<Edge<'_, T, S, A>> {
match self.path.pop() {
Some(edge_id) => {
self.head = self.graph.get_arc(edge_id).source;
@ -164,7 +150,7 @@ impl<'a, T: 'a + Hash + Eq + Clone, S: 'a, A: 'a> Stack<'a, T, S, A> {
}
/// Returns a read-only view of the head element.
pub fn head<'s>(&'s self) -> Node<'s, T, S, A> {
pub fn head(&self) -> Node<'_, T, S, A> {
Node::new(self.graph, self.head)
}
@ -235,14 +221,11 @@ impl<'a, T: 'a + Hash + Eq + Clone, S: 'a, A: 'a> Stack<'a, T, S, A> {
/// Returns the `i`th item of the path. Path items are indexed in order of
/// traversal (i.e., the last element is the path head).
pub fn item<'s>(&'s self, i: usize) -> Option<StackItem<'s, T, S, A>> {
pub fn item(&self, i: usize) -> Option<StackItem<'_, T, S, A>> {
if i == self.path.len() {
Some(StackItem::Head(self.head()))
} else {
match self.path.get(i) {
Some(edge_id) => Some(StackItem::Item(Edge::new(self.graph, *edge_id))),
None => None,
}
self.path.get(i).map(|edge_id| StackItem::Item(Edge::new(self.graph, *edge_id)))
}
}
}
@ -254,7 +237,7 @@ where
/// Creates a new path iterator from a borrow of a path.
fn new(path: &'s Stack<'a, T, S, A>) -> Self {
StackIter {
path: path,
path,
position: 0,
}
}

View File

@ -271,12 +271,7 @@ where
/// Returns a reference to an edge between the given nodes that is already in
/// the graph, or `None` if there is no such edge.
pub fn find_edge(&self, source: NodeRef<'id>, target: NodeRef<'id>) -> Option<EdgeRef<'id>> {
for child in self.children(source) {
if self.raw_edge(child).target == target.id {
return Some(child);
}
}
None
self.children(source).find(|child| self.raw_edge(*child).target == target.id)
}
/// Adds a node for the given game state with the given data, returning a
@ -348,7 +343,7 @@ where
/// Returns a reference to the game state that `node` is associated with.
pub fn node_state(&self, node: NodeRef<'id>) -> &T {
&self
self
.graph
.state_ids
.get_symbol(&node.id)
@ -478,8 +473,8 @@ where
}
/// As `retain_reachable_from`, but working over raw `VertexId`s.
fn retain_reachable_from_ids(mut self, root_ids: &[VertexId]) {
crate::mark_compact::Collector::retain_reachable(&mut self.graph, root_ids);
fn retain_reachable_from_ids(self, root_ids: &[VertexId]) {
crate::mark_compact::Collector::retain_reachable(self.graph, root_ids);
}
}