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

Arc<str> does not have a Default impl #124275

Closed
Billy-Sheppard opened this issue Apr 23, 2024 · 5 comments
Closed

Arc<str> does not have a Default impl #124275

Billy-Sheppard opened this issue Apr 23, 2024 · 5 comments
Labels
C-bug Category: This is a bug. C-feature-request Category: A feature request, i.e: not implemented / a PR. Libs-Small Libs issues that are considered "small" or self-contained T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Billy-Sheppard
Copy link
Contributor

Billy-Sheppard commented Apr 23, 2024

I tried this code:

    let my_var: Arc<str> = Default::default();
    assert_eq!(my_var, Arc::from(""));

I expected to see this happen:

Compiles and Runs successfully.

Instead, this happened:

error[E0277]: the trait bound `str: Default` is not satisfied
 --> src/main.rs:4:28
  |
4 |     let my_var: Arc<str> = Default::default();
  |                            ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `str`, which is required by `Arc<str>: Default`
  |
  = help: the following other types implement trait `Default`:
            &str
            &mut str
  = note: required for `Arc<str>` to implement `Default`

For more information about this error, try `rustc --explain E0277`.

It does work for Arc<&str> but not for Arc<str>. This is because str doesn't have a Default impl (presumably due to being unsized).

In theory its as "simple" as adding this into the std/alloc library, which does pass ./x.py check library/core on my machine. However this is definitely not the most efficient implementation of the intended result, and perhaps an implementation similar to impl Default for Box<str> would suffice more acutely.

#[cfg(not(no_global_oom_handling))]
impl Default for Arc<str> {
    /// Creates an empty str inside an Arc
    #[inline]
    fn default() -> Self {
        Arc::from("")
    }
}

Meta

rustc --version --verbose: Stable channel: Build using the Stable version: 1.77.2.
Applies on nightly: Nightly channel: Build using the Nightly version: 1.79.0-nightly (2024-04-21 fb898629a26e4acec59c) also.

@Billy-Sheppard Billy-Sheppard added the C-bug Category: This is a bug. label Apr 23, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 23, 2024
@Billy-Sheppard Billy-Sheppard changed the title Arc<str> does not have a Default impl Arc<str> does not have a Default impl Apr 23, 2024
@saethlin saethlin added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. Libs-Small Libs issues that are considered "small" or self-contained and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 23, 2024
@Aityz
Copy link

Aityz commented Apr 24, 2024

@rustbot claim

@Marcondiro
Copy link
Contributor

Hello @Billy-Sheppard, just out of curiosity, which use-case do you have in mind for this?
Thanks

@Aityz
Copy link

Aityz commented Apr 25, 2024

I believe implementing Default for Arc<str> in std::alloc is the most efficient solution to this problem, as Box<str> is already implemented.

@ryanavella
Copy link

Hello @Billy-Sheppard, just out of curiosity, which use-case do you have in mind for this? Thanks

Not OP, but I'd like to be able to quickly refactor Box<str> usages into Arc<str> without fighting the compiler too much.

@Billy-Sheppard
Copy link
Contributor Author

Hello @Billy-Sheppard, just out of curiosity, which use-case do you have in mind for this? Thanks

I have a shared Model struct for a WASM application, its quite useful to have Arc<str> rather than Arc<&str> or worse Arc<String> in. It's highly convenient to use the various impls of Default for initialising said Model.

(..Default::default() my beloved)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. C-feature-request Category: A feature request, i.e: not implemented / a PR. Libs-Small Libs issues that are considered "small" or self-contained T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants