Skip to content
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

Add trivial impl of Borrow<str> for Atom #266

Merged
merged 1 commit into from
Dec 16, 2022
Merged

Add trivial impl of Borrow<str> for Atom #266

merged 1 commit into from
Dec 16, 2022

Conversation

adamreichold
Copy link
Contributor

This enables Atom to be used in methods like HashMap::entry_ref.

@jdm
Copy link
Member

jdm commented Dec 14, 2022

@bors-servo r+
Thanks!

@bors-servo
Copy link
Contributor

📌 Commit 13db052 has been approved by jdm

@bors-servo
Copy link
Contributor

⌛ Testing commit 13db052 with merge 362ba45...

bors-servo added a commit that referenced this pull request Dec 14, 2022
Add trivial impl of Borrow<str> for Atom

This enables `Atom` to be used in methods like [`HashMap::entry_ref`](https://docs.rs/hashbrown/latest/hashbrown/hash_map/struct.HashMap.html#method.entry_ref).
@bors-servo
Copy link
Contributor

💔 Test failed - checks-github

@adamreichold
Copy link
Contributor Author

I committed a Cargo lockfile which references once_cell 1.14.0 instead of the current 1.16.0 and should be compatible with the MSRV of 1.49.0. I also adjust the CI workflow to use this lockfile when testing Rust 1.49.0 in the CI even I am not completely sure whether this will work for all parts of the CI workflow.

@jdm
Copy link
Member

jdm commented Dec 15, 2022

Oh, if the build failed it's because one of our dependencies now has a minimum Rust version that's higher than our version. There's nothing we can do about that except pin the older version in our dependencies, or update our MSRV. We just update our MSRV in that case.

@jdm
Copy link
Member

jdm commented Dec 15, 2022

#267

This enables Atom to be used in methods like HashMap::entry_ref.
@adamreichold
Copy link
Contributor Author

#267

I rebased on the post-merge master branch. I have to admit that I would not have bumped MSRV to follow once_cell as said project has apparently dropped the notion of keeping an MSRV.

@jdm
Copy link
Member

jdm commented Dec 15, 2022

@bors-servo r+

@bors-servo
Copy link
Contributor

📌 Commit 37b459f has been approved by jdm

@bors-servo
Copy link
Contributor

⌛ Testing commit 37b459f with merge 6c044c9...

@bors-servo
Copy link
Contributor

☀️ Test successful - checks-github
Approved by: jdm
Pushing 6c044c9 to master...

@bors-servo bors-servo merged commit 6c044c9 into servo:master Dec 16, 2022
@adamreichold adamreichold deleted the borrow-str branch December 16, 2022 06:00
bors-servo added a commit that referenced this pull request Feb 23, 2023
Revert trivial impl of Borrow<str> for Atom

#266 caused a breaking change (see #271) in 0.8.5 (which was yanked).
This PR fixes #271 / will allow publishing new versions (so #268 can make it out).

I've just started learning Rust, haven't been able to 100% wrap my head around the issue.

The breaking change manifesting in the "browserlist-rs" crate:
```rust
error[E0283]: type annotations needed
  --> {.. snip ..}\browserslist-rs-0.12.3\src\data\caniuse.rs:91:35
   |
91 |     let chrome = CANIUSE_BROWSERS.get(&"chrome".into()).unwrap();
   |                                   ^^^ ---------------- type must be known at this point
   |                                   |
   |                                   cannot infer type of the type parameter `Q` declared on the associated function `get`
   |
   = note: multiple `impl`s satisfying `Atom<BrowserNameAtomStaticSet>: Borrow<_>` found in the following crates: `core`, `string_cache`:
           - impl<Static> Borrow<str> for Atom<Static>
             where Static: StaticAtomSet;
           - impl<T> Borrow<T> for T
             where T: ?Sized;
note: required by a bound in `AHashMap::<K, V, S>::get`
  --> {.. snip ..}\ahash-0.7.6\src\hash_map.rs:81:12
   |
81 |         K: Borrow<Q>,
   |            ^^^^^^^^^ required by this bound in `AHashMap::<K, V, S>::get`
help: consider specifying the generic argument
   |
91 |     let chrome = CANIUSE_BROWSERS.get::<Q>(&"chrome".into()).unwrap();
   |                                      +++++
```

`CANIUSE_BROWSERS` is a
```rust
AHashMap<BrowserNameAtom, BrowserStat>
```

where `BrowserNameAtom` is an Atom generated with `string_cache_codegen`.

The signature of the `get` function:
```rust
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.0.get(k)
    }
```

`K` is the generic key type for the hash map.
This is the exact same signature as the std lib HashMap, to which it delegates.

When I debug 0.8.4 usage locally, the `From` that gets used for the `into()` is
```rust
impl<'a, Static: StaticAtomSet> From<&'a str> for Atom<Static> {
    #[inline]
    fn from(string_to_add: &str) -> Self {
        Atom::from(Cow::Borrowed(string_to_add))
    }
}
```

It isn't clear to me how the `Borrow<str>` impl that was added "competes" with the std lib's implementation here.
It doesn't seem relevant?
bors-servo added a commit that referenced this pull request Feb 23, 2023
Revert trivial impl of Borrow<str> for Atom

#266 caused a breaking change (see #271) in 0.8.5 (which was yanked).
This PR fixes #271 / will allow publishing new versions (so #268 can make it out).

I've just started learning Rust, haven't been able to 100% wrap my head around the issue.

The breaking change manifesting in the "browserlist-rs" crate:
```rust
error[E0283]: type annotations needed
  --> {.. snip ..}\browserslist-rs-0.12.3\src\data\caniuse.rs:91:35
   |
91 |     let chrome = CANIUSE_BROWSERS.get(&"chrome".into()).unwrap();
   |                                   ^^^ ---------------- type must be known at this point
   |                                   |
   |                                   cannot infer type of the type parameter `Q` declared on the associated function `get`
   |
   = note: multiple `impl`s satisfying `Atom<BrowserNameAtomStaticSet>: Borrow<_>` found in the following crates: `core`, `string_cache`:
           - impl<Static> Borrow<str> for Atom<Static>
             where Static: StaticAtomSet;
           - impl<T> Borrow<T> for T
             where T: ?Sized;
note: required by a bound in `AHashMap::<K, V, S>::get`
  --> {.. snip ..}\ahash-0.7.6\src\hash_map.rs:81:12
   |
81 |         K: Borrow<Q>,
   |            ^^^^^^^^^ required by this bound in `AHashMap::<K, V, S>::get`
help: consider specifying the generic argument
   |
91 |     let chrome = CANIUSE_BROWSERS.get::<Q>(&"chrome".into()).unwrap();
   |                                      +++++
```

`CANIUSE_BROWSERS` is a
```rust
AHashMap<BrowserNameAtom, BrowserStat>
```

where `BrowserNameAtom` is an Atom generated with `string_cache_codegen`.

The signature of the `get` function:
```rust
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.0.get(k)
    }
```

`K` is the generic key type for the hash map.
This is the exact same signature as the std lib HashMap, to which it delegates.

When I debug 0.8.4 usage locally, the `From` that gets used for the `into()` is
```rust
impl<'a, Static: StaticAtomSet> From<&'a str> for Atom<Static> {
    #[inline]
    fn from(string_to_add: &str) -> Self {
        Atom::from(Cow::Borrowed(string_to_add))
    }
}
```

It isn't clear to me how the `Borrow<str>` impl that was added "competes" with the std lib's implementation here.
It doesn't seem relevant?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants