-
Notifications
You must be signed in to change notification settings - Fork 2k
chore: replace anymap with a simple hashmap #19335
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,99 @@ | ||
| //! ExtraContext is used for passing extra data to Vector's components when Vector is used as a library. | ||
| use std::{ | ||
| any::{Any, TypeId}, | ||
| collections::HashMap, | ||
| marker::{Send, Sync}, | ||
| sync::Arc, | ||
| }; | ||
|
|
||
| use anymap::{ | ||
| any::{Any, IntoBox}, | ||
| Map, | ||
| }; | ||
|
|
||
| /// Structure containing any extra data. | ||
| /// The data is held in an [`Arc`] so is cheap to clone. | ||
| #[derive(Clone)] | ||
| pub struct ExtraContext(Arc<Map<dyn Any + Send + Sync>>); | ||
|
|
||
| impl Default for ExtraContext { | ||
| fn default() -> Self { | ||
| Self(Arc::new(Map::new())) | ||
| } | ||
| } | ||
| #[derive(Clone, Default)] | ||
| pub struct ExtraContext(Arc<HashMap<TypeId, Box<dyn Any + Send + Sync>>>); | ||
|
|
||
| impl ExtraContext { | ||
| /// Create a new `ExtraContext` with the provided [`anymap::Map`]. | ||
| pub fn new(context: Map<dyn Any + Send + Sync>) -> Self { | ||
| /// Create a new `ExtraContext` with the provided [`HashMap`]. | ||
| pub fn new(context: HashMap<TypeId, Box<dyn Any + Send + Sync>>) -> Self { | ||
| Self(Arc::new(context)) | ||
| } | ||
|
|
||
| /// Create a new `ExtraContext` that contains the single passed in value. | ||
| pub fn single_value<T: Any + Send + Sync>(value: T) -> Self { | ||
| let mut map = Map::new(); | ||
| map.insert(value); | ||
| let mut map = HashMap::new(); | ||
| map.insert(value.type_id(), Box::new(value) as _); | ||
| Self(Arc::new(map)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| /// This is only available for tests due to panic implications of making an Arc | ||
| /// mutable when there may be multiple references to it. | ||
| fn insert<T: Any + Send + Sync>(&mut self, value: T) { | ||
| Arc::get_mut(&mut self.0) | ||
| .expect("only insert into extra context when there is a single reference to it") | ||
| .insert(value.type_id(), Box::new(value)); | ||
| } | ||
|
|
||
| /// Get an object from the context. | ||
| pub fn get<T>(&self) -> Option<&T> | ||
| where | ||
| T: IntoBox<dyn Any + Send + Sync>, | ||
| { | ||
| self.0.get() | ||
| pub fn get<T: 'static>(&self) -> Option<&T> { | ||
| self.0 | ||
| .get(&TypeId::of::<T>()) | ||
| .and_then(|t| t.downcast_ref()) | ||
| } | ||
|
|
||
| /// Get an object from the context, if it doesn't exist return the default. | ||
| pub fn get_or_default<T>(&self) -> T | ||
| pub fn get_or_default<T: 'static>(&self) -> T | ||
| where | ||
| T: IntoBox<dyn Any + Send + Sync> + Clone + Default, | ||
| T: Clone + Default, | ||
| { | ||
| self.0.get().cloned().unwrap_or_default() | ||
| self.get().cloned().unwrap_or_default() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[derive(Clone, Eq, PartialEq, Debug, Default)] | ||
| struct Peas { | ||
| beans: usize, | ||
| } | ||
|
|
||
| #[derive(Clone, Eq, PartialEq, Debug, Default)] | ||
| struct Potatoes(usize); | ||
|
|
||
| #[test] | ||
| fn get_fetches_item() { | ||
| let peas = Peas { beans: 42 }; | ||
| let potatoes = Potatoes(8); | ||
|
|
||
| let mut context = ExtraContext::default(); | ||
| context.insert(peas); | ||
| context.insert(potatoes); | ||
|
|
||
| assert_eq!(&Peas { beans: 42 }, context.get::<Peas>().unwrap()); | ||
| assert_eq!(&Potatoes(8), context.get::<Potatoes>().unwrap()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn get_or_default_fetches_item() { | ||
| let potatoes = Potatoes(8); | ||
|
|
||
| let mut context = ExtraContext::default(); | ||
| context.insert(potatoes); | ||
|
|
||
| assert_eq!(Potatoes(8), context.get_or_default::<Potatoes>()); | ||
| assert_eq!(Peas::default(), context.get_or_default::<Peas>()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn duplicate_types() { | ||
| let potatoes = Potatoes(8); | ||
| let potatoes99 = Potatoes(99); | ||
|
|
||
| let mut context = ExtraContext::default(); | ||
| context.insert(potatoes); | ||
| context.insert(potatoes99); | ||
|
|
||
| assert_eq!(&Potatoes(99), context.get::<Potatoes>().unwrap()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Would it be worth adding a ban to
deny.tomlfor this?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.
That's a good idea 👍