Comments (8)
Hmm, after looking at it a bit, it seems that the Cookie
struct uses Cow
so it can either own the strings or have just references. If it owns the stuff, the lifetime of the returned strings naturally should not exceed the lifetime of Cookie
, so it might or might not be tied to the lifetime of Cookie
.
from cookie-rs.
Is there any widely-used enum
in the ecosystem that is used to have two different lifetimes? A bit like Cow
but for borrowing from it, so that there would be different lifetimes for a variant that has been borrowed from the string owned by Cow
, and a variant that has been re-borrowed from a reference in Cow
. That would sound the right thing to do in this situation, but I've never encountered such an enum
. Should we open a thread to some community forum to discuss borrowing from Cow
more widely?
from cookie-rs.
I made some some experiments here with such an enum. (Called it VarLifetimes
) https://github.com/golddranks/cookie-rs/tree/experiments It seems like it'd deserve its own crate though. Are you aware of any already existing similar things? This kind of a thing might be valuable with libraries that want to support optionally zero copy parsing things.
from cookie-rs.
Yeah I think the only recourse here would be to expose the underlying storage. There'd need to be a method that returns both variants, and at runtime you'd assert that the 'c
lifetime was the one returned, not the &self
lifetime.
from cookie-rs.
In my experiment I made it return an enum
that contained a short (&'s self
) and a long (&'c
) lifetime. Both are always coercible to the shorter one, so a helper method .short()
returning a ref with the short lifetime isn't too bad ergonomics-wise. Using that one can always use the returned ref as it currently is, i.e. with the lifetime of a self ref to Cookie
. I also implemented Deref
which allows calling methods on the ref like one would currently call on the returned ref.
However, the ergonomic sugar ends there – I tried to implement the Eq
traits too to make the tests pass without adding any explicit calls to the .short()
method, but it seems that Option<T>
and types like that are hard. The PartialEq
impls on them are implemented only for the same inner type, and I'm unable to implement PartialEq
between Option<&T> and VarLifetimes<'short, 'long, T>
because of coherence issues.
That means that assert_eq!(Some("www.rust-lang.org"), cookie.domain())
isn't possible without calling the helper method: assert_eq!(Some("www.rust-lang.org"), cookie.domain().map(|d| d.short()))
Then again, I'm not sure how "transparent" a double-lifetime wrapper type like VarLifetimes
should be, if it or something like it would be actually used.
from cookie-rs.
Another, possibly better possibility is to have simply a another set of methods on Cookie
that return Option<&'long str>
.
from cookie-rs.
Yeah so that actually sounds like the best option, Option
-based accessors that maybe return a "long" string where otherwise the infallible operation with a "short" lifetime is always available too
from cookie-rs.
Closed by 74b9002.
from cookie-rs.
Related Issues (20)
- Nightly detection does not take into account whether features can actually be used HOT 9
- Panic when verifying malformed signed cookie HOT 2
- Parse multiple cookies in single string? HOT 1
- 0.16 release HOT 3
- Replace base64 with base64ct HOT 5
- Iterator over all cookies from string HOT 1
- Removing cookies by name HOT 4
- Private, signed & key methods missing
- Why was ring removed? HOT 1
- Commas are not encoded correctly
- Cookie builder doesn't ignore leading dots (as the `FromStr` implementation does) HOT 4
- Use `std::time::Duration` instead of `time::duration::Duration` for `max-age` HOT 1
- Trait bound error after upgrading to 0.17.0 HOT 1
- Support for `__Host-` cookies HOT 4
- Set Removal Cookies SameSite to Lax HOT 3
- Question : SignedJar::verify_result HOT 2
- aes-gcm vulnerability HOT 1
- Additional Message Data for signing only. HOT 3
- Does cookie-rs support cookie "Partitioned" yet, please? HOT 1
- Custom Extensions in the Set-Cookie String
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cookie-rs.