-
Notifications
You must be signed in to change notification settings - Fork 77
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
Move out the list of static strings? #22
Comments
That's definitely the plan - @kmcallister and I have discussed briefly, he may have some thoughts on how this could be done too. |
I'd really like to see this implemented, or at least hear your thoughts about how it could be done. |
Here is the plan: every atom will be of a certain "kind" (which will be part of its type), each kind will have its own static set (and therefore atoms of different kinds representing different strings may have the same internal representation), and only atoms of the same kind are comparable. This will generalize the type safety of the current #[derive(Hash, PartialEq, Eq)]
pub struct Atom<K=DefaultKind> where K: Kind {
data: u64,
kind: PhantomData<K>,
}
pub trait Kind {
fn str_to_id(s: &str) -> Option<u32>;
/// Should panic for unknown IDs
fn id_to_str(id: u32) -> &'static str;
}
pub enum DefaultKind {}
impl Kind for DefaultKind {
#[inline] fn str_to_id(_: &str) -> Option<u32> { None }
#[inline] fn id_to_str(_: u32) -> &'static str { panic!("bad static atom") }
} A
This will be the only way to get static atoms. The compiler plugin will be removed. html5ever will define various "kinds" for element names, attribute names, namespace URLs, etc. Servo’s style crate will be able to generate one (always up-to-date) for CSS property names. How does this sound? @glennw, @jdm, @Ms2ger, @mbrubeck, @Manishearth |
Sounds great to me! |
Alternative: pub trait Kind {
fn static_atom_set() -> &'static phf::OrderedSet<&'static str>;
} The set for |
I don't know enough about how string-cache works, but this seems sensible enough. |
One thing is I don’t know where things like #83 would fit. Many separate "kinds"? |
I've implemented #22 (comment) (created a new crate, made necessary changes to this crate, got all the tests working and kept it backwards compatible). Unfortunately it requires the Note that "backwards compatible" means "keeps the static atoms in the crate for now, so servo can upgrade then move to the new system at leisure". Since there's a delay, I have a question for the eventual submission - where will the new |
Breaking backward-compatibility of a library is not necessarily a problem for Servo, which can keep using an old version for a while. Thanks for working on this, but even though I proposed it, I’m not so sure now this design is such a good idea. Atoms of different kinds won’t be comparable, and having to specify On the other hand, centralizing a list of strings is theoretically not nice but it’s not such a problem in practice. Potential users of this library should feel free to send PRs to add static strings. The only marginal cost is the size of compiled binaries: the strings themselves + a few integers each. (The run-time cost of hash table lookups is O(1).) |
The backwards compatibility was more about being able to upgrade, then being able to incrementally move things over (rather than one huge servo PR).
This seems like a good thing to me. If you've got multiple static sets of strings, trying to compare them will give incorrect results so you actually want the typechecker to help.
You could just use the type alias for custom atom types, e.g. I think I've thought of a way to work around the lack of RFC, so I'll send a PR 'shortly'. |
CC @bholley It would be nice to have the static string list in servo/servo, but would that not make it impossible to use static atoms in servo's dependencies? xml5ever and rust-selectors use a few, and html5ever rather more. |
The idea is that each crate could define one (or more) atom kind(s), each associated with a set of static strings. Not everything would be in servo/servo. |
So when html5ever creates an element, it would create one of its atoms, which we'd need to convert to some other kind to match selectors against it? |
That’s a good point. We might have wanted an |
Yes, I'd expect all different 'kinds' of atoms to define their own list of atoms, and anything that needs sharing between multiple crates would be expressed as a common dependency. A bit like when defining enums you don't create one type to hold every possible variant regardless of what's it's used for (since you're subverting the type system) - instead, you create multiple types with variants split in some logical way. I'm not sure how you'd enforce that there's only one copy of this dependency and that every depending crate uses this one copy (the same problem would exist with enums as well)...I'd expect Cargo to have some way of helping here. On reading through that linked issue, perhaps Edit: in fact, you already have to deal with the above problem with string-cache |
It’s going into various atom types in html5ever and Servo.
It’s going into various atom types in html5ever and Servo.
It’s going into various atom types in html5ever and Servo.
It’s going into various atom types in html5ever and Servo.
Make Atom generic over the set of static strings The new `string_cache_codegen` crate is intended to be used by downstream users in their build scripts. Provided a list of static strings, it generates code that defines: - A type alias for `Atom<_>` with the type parameter set, - A macro like the former `atom!` macro. Based on #136, original work by @aidanhs. Fixes #22, fixes #136, closes #83. r? @nox <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/string-cache/178) <!-- Reviewable:end -->
Currently, this repository contains an hard-coded list of static strings that is tailored for Servo. Would it be possible to move this list out of string-cache so that Servo and other potential users can maintain their own version of it? Perhaps by making
Atom
generic over a type with a trait that provides the list as an associated static item.The text was updated successfully, but these errors were encountered: