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.
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 Cow<'static, str> for Metadata strings #1020
Use Cow<'static, str> for Metadata strings #1020
Changes from 19 commits
5868a6d
f40de7b
ed04e30
645c133
5a69f07
f922cc1
fa4a808
183f75f
97e5b31
028ae65
a3661f8
f0a6443
a9a0568
2ee5da9
d8afcbe
381d574
8913759
b250109
e29d3ca
f7274dc
b3bec86
fda189b
c46c352
7b0dfe5
280f0bf
4225d96
f42a644
47e9cd5
de588f4
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
One fairly major issue with making this change: we've just changed
tracing
andtracing-core
to support no-std platforms that don't haveliballoc
. Unfortunately,Cow
is defined inliballoc
. We'll need to change this to work gracefully onno_std
platforms without alloc.We now have an "alloc" feature flag for determining whether liballoc is supported, and APIs that depend on alloc can now require that feature. That should make it possible to conditionally support
Cow
only when alloc is availble, although it does make the API surface a little bit uglier.Here's what I would recommend:
Change the fields to something like this:
We can't change the return type of API functions based on whether or not a feature flag is enabled, since this would make enabling the feature break code in crates that don't enable it. Therefore, we can't have methods like
Metadata::file()
andMetadata::line()
returnOption<&str>
when alloc is disabled, andOption<Cow<'a, str>>
when it is enabled. Instead, we'll need two separate APIs: one which is always available but only returns borrowed strings, and another which returnsCow
s and is only available when the "alloc" feature is enabled.I would expect the implementation to look something like this:
(...and so on for the other fields).
Note that I don't particularly care for the name
file_cow
(andtarget_cow
etc), but I don't know if there are better options ---file_as_cow
feels a little wordy? If you can think of a better naming scheme for these, we can go with that.Finally, I think we'll need two constructors. The
const fn new
will continue to take '&str's only, and always be available, regardless of features. We'll add a new constructor which takesCow
s (orimpl Into<Cow<'a, str>>
s?), and have that constructor only be exposed when the "alloc" feature is enabled.Unfortunately, this makes the API a little less pleasant to work with, but it's the only way to feature flag the use of
Cow
without making a non-additive feature that breaks code when it's enabled...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.
Thank you for the detailed feedback, much appreciated!
I made the change as suggested under 1) and while a bit ugly it works fine.
As for your second point I don't think it's necessarily a problem: as long as we can build a
Metadata
with dynamic data, it's fine to keep returning&str
. So for example we keepMetadata::file
returning anOption<&str>
regardless of how it was built. FWIW I started this work thinking we'd need to duplicate the API and have methods returnCow
s, but @gnunicorn suggested we just keep things as they were. Do you think there's a need for/value in having the*_cow()
methods?I also added a
from_cow
constructor for your third point, featured gated. Struggling a bit to come up with a sensible test for it though.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.
yeah, I really can't think of a good reason, why we'd need to expose the
cow
(in particular if non-mut) over just keeping the current expose functions.Maybe re 2, I'd have the suggestion of just having two API-equivialent
impl
(and maybe evenstruct
) blocks one for when the feature is activated and one without – and the earlier has the extra constructor for non-static-string. I think that would improve legibility quite a bit.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 tried this and while the code does look better, I think I would need to duplicate the docs which increases the maintenance burden.
I think it's better to have an easier time maintaining the docs than more readable code in this case, but I'm easy and can do either.
@hawkw thoughts?
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.
while with this we now have general support for dynamic creation, the current API, still won't allow for that. Probably want to add another
fn dynamic_new
where we can passCow<'a, str>
into for dynamic dispatching.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 am afraid so ....