-
Notifications
You must be signed in to change notification settings - Fork 20
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
construct NamedField
s from owned Strings
#73
Conversation
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Depends on #73. Currently, feature-flagged public APIs will not be documented as requiring feature flags. This branch adds `#[doc(cfg(...))]` to all feature-flagged APIs. For the most part, this is done using the `feature!` macro lifted from Tokio: https://github.com/tokio-rs/tokio/blob/8943e8aeef0b33f371d6dc69f62b38da390b5d5f/tokio/src/macros/cfg.rs#L3-L14 In a couple of places, though, the feature-flagged thing is in a position other than item position, so in that case, it was necessary to just paste the attribute... Signed-off-by: Eliza Weisman <[email protected]>
hmm...this CI failure seems concerning: https://github.com/tokio-rs/valuable/runs/4291553819?check_suite_focus=true#step:4:566 This approach may end up not being viable. But, I'm also uncertain about the error in that case; AFAICT the value should not be dropped inside the static? I think we may end up having to put the named fields in another static to fix this, and reference it in the variant static...which kinda sucks. I'm not sure if there's a great way to support owned string values here without @carllerche & @taiki-e, any thoughts? |
Signed-off-by: Eliza Weisman <[email protected]>
Continuing to look into this, it looks like the issue occurs with It seems like the cause of the issue is that the compiler cannot tell that, when the However, the problem can be trivially solved by putting the The other solution to #62 would be to have the owned variant not use Therefore, IMO, requiring I'd love to hear what @taiki-e and @carllerche think is the right way to move forward here. |
This is a solid PR, but the wrong direction IMO. The To better handle the dynamic case, I think we can, instead, provide additional field accessor methods that take fn visit_field(&self, name: &str, visit: impl Visit); I don't know if we can have the fn return a
|
Going to close as I don't think we will move forward w/ this direction. Thanks for exploring 👍 |
Issue #62 describes a need to construct
Fields::Named
from an ownedset of strings, rather than borrowed ones. This branch adds a
NamedField::from_string
constructor that returns a'static
NamedField
constructed from an ownedString
value.The proposed solution in #62 is to use
Cow
s (presumably constructingNamedField
fromT: Into<Cow<'a, str>>
). However, we can't easily dothis, as
Cow
is not available withoutalloc
, so we'd need twototally separate internal representations based on whether
alloc
isenabled. Additionally, we can't have a single
new
constructor takingT: Into<Cow<'a, str>>
, for a couple reasons: first, thenew
constructor is a
const fn
, andconst fn
s can't currently have traitobunds on generic arguments; second, we can't have the
new
constructorexpose
Cow
, since the constructor would have to have a different typesignature when the
alloc
feature is not enabled.Instead, I've added an internal enum that's roughly
Cow
-like, and aNamedField::from_string
constructor taking aString
by value. Ratherthan directly mimicking
Cow
, though, the owned string case is wrappedin an
Arc
. This is becauseNamedField
currently implementsClone
and
Copy
; it's intended to be cheaply clonable. It seems quite badto now have a
Clone
that's either a basically free pointer copy or,sometimes, allocating a string and copying all the bytes in it. Instead,
when a
NamedField
is constructed from a string, we reference count itso that it can be cloned inexpensively.
I did still remove the
Copy
impl forNamedField
, sinceArc
s arenot
Copy
. However, if we think it's acceptable for an ownedNamedField
to perform an arc refcount bump and still implementCopy
,I could put it back.
Hopefully, this solves some of the problem from #62. I think we might
also need to do something similar for
Fields::Named
, so that the arraycan either be a borrowed slice or an owned
Vec<NamedField>
/Box<[NamedField]>
. We can do this with a similarapproach as the one used here, but I wanted to put this PR up first
before starting on that, to make sure it's basically the right
direction.