-
Notifications
You must be signed in to change notification settings - Fork 6
Refactor/clippy warnings #18
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
base: main
Are you sure you want to change the base?
Conversation
|
One warning given by clippy is about type complexity. Example ( struct HybridParentingWorkArea<NM: NodeManager, CM: ContactManager> {
/// The bundle associated with this work area.
pub bundle: Bundle,
/// The source route stage, representing the starting point for routing.
pub source: Rc<RefCell<RouteStage<NM, CM>>>,
/// A sorted list of node IDs to be excluded from routing paths.
pub excluded_nodes_sorted: Vec<NodeID>,
/// A vector containing vectors of route stages, grouped by destination.
/// Each inner vector represents possible routes to a specific destination,
/// sorted in order of preference.
pub by_destination: Vec<Vec<Rc<RefCell<RouteStage<NM, CM>>>>>,
}We can replace with: struct HybridParentingWorkArea<NM: NodeManager, CM: ContactManager> {
...
pub by_destination: Vec<Vec<SharedRouteStage<NM, CM>>>,
}
pub type SharedRouteStage<NM: NodeManager, CM: ContactManager> = Rc<RefCell<RouteStage<NM, CM>>>;However, we are warned about a current limitation of the type checker: warning: bounds on generic parameters in type aliases are not enforced
--> src/pathfinding/hybrid_parenting.rs:79:31
|
79 | pub type SharedRouteStage<NM: NodeManager, CM: ContactManager> = Rc<RefCell<RouteStage<NM, CM>>>;
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^ will not be checked at usage sites of the type alias
|
= note: this is a known limitation of the type checker that may be lifted in a future edition.
see issue #112792 <https://github.com/rust-lang/rust/issues/112792> for more information
= note: `#[warn(type_alias_bounds)]` on by default
help: remove these bounds
|
79 - pub type SharedRouteStage<NM: NodeManager, CM: ContactManager> = Rc<RefCell<RouteStage<NM, CM>>>;
79 + pub type SharedRouteStage<NM, CM> = Rc<RefCell<RouteStage<NM, CM>>>;So, I won't simplify the types if it means that the type checker won't work (plus it generates this other warning). |
|
There's also tons of macros that reference using E.g.: warning: `crate` references the macro call's crate
--> src/pathfinding/limiting_contact/mod.rs:100:17
|
100 | NM: crate::node_manager::NodeManager,
| ^^^^^ help: to reference the macro definition's crate, use: `$crate`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.91.0/index.html#crate_in_macro_def i.e. "crate refers to the macro call’s crate, whereas $crate refers to the macro definition’s crate. Rarely is the former intended. See: https://doc.rust-lang.org/reference/macros-by-example.html#hygiene" I'm not sure if the A-SABR macros actually make use of this, so I won't touch it as it could modify the code behaviour. |
Implement Default for types with a new() method.
|
This only fixes lints that afaik don't have any side effects on the business logic. Ready for review. |
Yes because I don't think the type annotation is required here (you can see the original code doesn't have the pub type SharedRouteStage<NM, CM> = Rc<RefCell<RouteStage<NM, CM>>>;seems valid to me, and any type (e.g. a struct) that uses this alias will enforce the trait requirement, e.g. : struct HybridParentingWorkArea<NM: NodeManager, CM: ContactManager> {
...
pub by_destination: Vec<Vec<SharedRouteStage<NM, CM>>>, // CM: ContactManager enforced by struct def
} |
| /// A hashmap that stores the coercion functions with their associated markers. | ||
| map: HashMap<&'a str, T>, | ||
| } | ||
| impl<'a, T> Default for Dispatcher<'a, T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
used somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clippy recommends implementing Default for types that have a new() method, because "The user might expect to be able to use Default as the type can be constructed without arguments."
Since RoutingTable is part of the public API of a-sabr, it would be used by users of a-sabr to fill all or some of the struct fields automatically with Default::default() for convenience.
Reference: https://rust-lang.github.io/rust-clippy/rust-1.91.0/index.html#new_without_default
However if this is not particularly convenient or important at the moment, just tell me to and I'll remove the commit.
| _phantom_distance: PhantomData<D>, | ||
| } | ||
|
|
||
| impl<NM: NodeManager, CM: ContactManager, D: Distance<NM, CM>> Default for RoutingTable<NM, CM, D> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
|
Thanks for the review, I'll add type aliases |
face6e0 to
58e76ff
Compare
|
Here's a question I have:
pub type ContactMarkerMap<'a> = Dispatcher<'a, ContactDispatcher>;
pub type NodeMarkerMap<'a> = Dispatcher<'a, NodeDispatcher>;
pub type ContactDispatcher = fn(&mut dyn Lexer) -> ParsingState<Box<dyn ContactManager>>;
pub type NodeDispatcher = fn(&mut dyn Lexer) -> ParsingState<Box<dyn NodeManager>>;[Node,Contact]MarkerMap are used throughout the code. E.g.: ( ...
node_marker_map: Option<&Dispatcher<'_, fn(&mut dyn Lexer) -> ParsingState<NM>>>,
...These appear in multiple places in the code, and sometime give out "type complexity" warnings from Clippy. To address this, I was thinking of defining the following public types in pub type StaticNodeMarkerMap<'a, NM> = Dispatcher<'a, StaticNodeDispatcher<NM>>;
pub type StaticNodeDispatcher<NM> = fn(&mut dyn Lexer) -> ParsingState<NM>;And similar for Contact[*]. Do you think that's a good idea? Is the naming OK? |
|
Since the code is the same for both, I could do a generic: pub type StaticMarkerMap<'a, M> = Dispatcher<'a, StaticDispatcher<M>>;
pub type StaticDispatcher<M> = fn(&mut dyn Lexer) -> ParsingState<M>;M stands for manager (contact or node) E.g., in pub fn parse<
NM: NodeManager + DispatchParser<NM> + Parser<NM>,
CM: ContactManager + DispatchParser<CM> + Parser<CM>,
>(
lexer: &mut dyn Lexer,
node_marker_map: Option<&Dispatcher<fn(&mut dyn Lexer) -> ParsingState<NM>>>,
contact_marker_map: Option<&Dispatcher<fn(&mut dyn Lexer) -> ParsingState<CM>>>,
) -> Result<(Vec<Node<NM>>, Vec<Contact<NM, CM>>), String> {
...
}Becomes: pub fn parse<
NM: NodeManager + DispatchParser<NM> + Parser<NM>,
CM: ContactManager + DispatchParser<CM> + Parser<CM>,
>(
lexer: &mut dyn Lexer,
node_marker_map: Option<&StaticMarkerMap<NM>>,
contact_marker_map: Option<&StaticMarkerMap<CM>>,
) -> Result<(Vec<Node<NM>>, Vec<Contact<NM, CM>>), String> {
...
} |
d2ae65d to
1608560
Compare
|
I have added a few commits with some typedefs (public and private) and a doc comment correction. |
Clippy warned about type complexity regarding Rc<RefCell<RouteStage>>>. This commit fixes some of these specific warnings. This commit introduces a new public type `SharedRouteStage`: `pub type SharedRouteStage<NM, CM> = Rc<RefCell<RouteStage<NM, CM>>>;` And replaces the code where it felt it improved readability / cognitive load.
Clippy warned about type complexity where the following, or similar, appears: `Option<&Dispatcher<fn(&mut dyn Lexer) -> ParsingState<T>>>,` Which raises the warnings. This commit replaces it with: `Option<&StaticMarkerMap<T>>,` This commit defines the following in src/parsing.rs, alongside similar definitions: `pub type StaticMarkerMap<'a, M> = Dispatcher<'a, StaticDispatcher<M>>;` `pub type StaticDispatcher<M> = fn(&mut dyn Lexer) -> ParsingState<M>;` The naming was chosen as similar type definitions exist, but with dyn trait instead of generics.
The types in the code did not match those described in that doc comment. This commit attemps to fix this doc comment.
Clippy warned about type complexity where the following, or similar,
appears:
`pub first_hops: HashMap<usize, (Rc<RefCell<Contact<NM, CM>>>, Vec<SharedRouteStage<NM, CM>>)>,`
Which raises the warnings.
This commit reduces it to:
`pub first_hops: HashMap<usize, FirstHopsVec<NM, CM>>,`
By introducing the following *private* types in `src/parsing.rs`, alongside similar
definitions:
```
type FirstHopsVec<NM, CM> = (
Rc<RefCell<Contact<NM, CM>>>,
Vec<Rc<RefCell<RouteStage<NM, CM>>>>,
);
type FirstHop<NM, CM> = (
Rc<RefCell<Contact<NM, CM>>>,
Rc<RefCell<RouteStage<NM, CM>>>,
);
```
The naming was chosen from the naming of attributes that used those
types.
To not mistake the plural form and to reflect the inner type, its
naming ends with 'Vec'.
The two new types are private because I did not find a use for them
outside the routing module.
Clippy warns about the complexity of such types: `Result<(Vec<Node<NM>>, Vec<Contact<NM, CM>>), String>` This commit reduces it to: `Result<ContactPlan<NM, NM, CM>, String>` By defining a new `ContactPlan` type in `contact_plan/mod.rs`: `type ContactPlan<NNM, CNM, CCM> = (Vec<Node<NNM>>, Vec<Contact<CNM, CCM>>);` The name was inspired from `exercises/0-ion-tvgutil-parsing/README.md:104`
This commit introduces a new *public* type: `type SharedPathFindingOutput<NM, CM> = Rc<RefCell<PathFindingOutput<NM, CM>>>;` for convenience, and to lower cognitive load where clippy deems necessary. This commit only applies this type in a single place, one where clippy generates a warning.
a432a69 to
1246f94
Compare
|
I ran some benchmarks to make sure everything was OK.
I ran this last one three times, and the results seem to suggest that performance was not or minimally impacted? E.g. with Benchmark 2: Benchmark 3: I also ran the following on main and on this branch, and both ran fine with seemingly no difference between the two: cargo run --example bundle_processing --features node_proc
cargo run --example contact_plans
cargo run --example dijkstra_accuracy --features contact_work_area
cargo run --example eto_management --all-features
cargo run --example satellite_constellation --features node_tx |
This PR addresses
cargo clippy --all-featureswarnings.