-
Notifications
You must be signed in to change notification settings - Fork 29
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
use a compile time default matcher map #11
Conversation
This change makes the default map of matchers defined at compile time, making `Infer::new` much cheaper for high workload.
Looks good to me. This would make it easy in the future to put |
Thinking more about it we could make static INFER: Infer = Infer::new(); |
Why not ! I was just writing top level methods (that With top level methods the API would be consumed this way: // not necessary but this is just to highlight that here infer is an external crate.
extern crate infer;
fn my_func(buf: &[u8]) {
match infer::get(buf) {
Some(t) => do_other_stuff(buf),
None => do_no_type_stuff(buf),
}
} With // not necessary but this is just to highlight that here infer is an external crate.
extern crate infer;
use infer::INFER;
fn my_func(buf: &[u8]) {
match INFER.get(buf) {
Some(t) => do_other_stuff(buf),
None => do_no_type_stuff(buf),
}
} I am totally fine with both, the later being very straightforward to implement. I could add a commit to this PR if you wish. |
Let's see what @bojand thinks about it |
Forget about this, it works with a trick and without changing the API. I have some code ready for that if you want to play |
I think I might have a workaround around it. I'll try it in the next few days. |
Sorry I updated my comment at the same time. Indeed there is a workaround. I have an implementation for it. I will submit a PR once we have some feedbacks from @bojand. |
MATCHER_MAP | ||
.iter() | ||
.map(|(mt, mime, ext, matcher)| (mt, *mime, *ext, matcher)) | ||
.chain(custom) |
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.
Does it make more sense to chain MATCHER_MAP
to custom
iterator? Idea being that if clients add custom matchers, perhaps we should evaluate those first before we evaluate the standard ones. Just a thought.
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.
I fully agree but I chose to keep the current behavior (custom matchers were pushed to the vector). Since we all agree I will push a new commit to this PR that does the change, but you will need to bump the Infer
release to a major number (0.2 -> 0.3) since this change might affect current users. I will document that in the API.
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.
We'll probably have to do a 0.3 release anyway if we add no_std support, but it's probably better to leave it as is for the moment just so we can do one last 0.2 release integrating these changes and any other non breaking changes that might come up before the 0.3 release
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.
It is up to you guys, the commit is ready to be pushed to this PR or another one if you prefer.
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.
You are right... there is a semver-breaking implication to that change. Yup lets do the release dance and keep the breaking change out of this PR and release this change as a patch version; and then we can introduce the behaviour change in subsequent release where we're introducing API changes anyway.
Hello, and thanks for the PR! I really appreciate the improvements and the collaboration 🙏. I left a couple of minor comments / questions regarding the proposed changes, but otherwise looks good! When we address those we can merge this PR. First a caveat, I don't do Rust development on a daily basis and context switch is sometimes difficult and I forget some of the mechanics of language or syntax, idioms, etc... With regards to "top-level" API... extern crate infer;
fn my_func(buf: &[u8]) {
match infer::get(buf) {
Some(t) => do_other_stuff(buf),
None => do_no_type_stuff(buf),
}
} Just so I fully understand... what would the implementation of such API look like in the crate? All functions would be just be exported crate functions that refer to Purely from aesthetic perspective, perhaps this is a little bit "nicer" than having to initialize a new instance or refer to a static instance? But I think that's subjective or debatable. Additionally wouldn't that have the (debatably relatively minor) advantage of not having an Infer instance allocated and created potentially at all if the client does not need custom matchers...? The static instance change is relatively simple, whereas the "top-level-api-wrapped-in-instance" approach seems like a bit more rewriting and rewiring. I feel either approach is beneficial and good, so I do not feel strongly about either one. Whatever makes sense holistically, and follows established practices is probably good. |
Thanks for
This is exactly the idea.
This is indeed debatable. I also believe that this is a little bit "nicer" to use: you do not need to
Exactly
Yes, but we are talking about couple of minutes here. I would be pleased to do the change since I need it for an internal project of mine.
I would favor the top level functions instead of |
I guess we could avoid exporting |
I believe this is a great compromise. |
This change makes the default map of matchers defined at compile time, making
Infer::new
much cheaper for high workload.This change should be (please double check) fully compatible as far as SemVer is concerned.
This also allows for further enhancement such having a completely static Infer experience with functions at crate level.
Infer::new
would only be needed when custom matchers are needed. I will probably submit a PR for this after this one.Cheers