GithubHelp home page GithubHelp logo

smol_str's Introduction

rust-analyzer logo

rust-analyzer is a modular compiler frontend for the Rust language. It is a part of a larger rls-2.0 effort to create excellent IDE support for Rust.

Quick Start

https://rust-analyzer.github.io/manual.html#installation

Documentation

If you want to contribute to rust-analyzer check out the CONTRIBUTING.md or if you are just curious about how things work under the hood, check the ./docs/dev folder.

If you want to use rust-analyzer's language server with your editor of choice, check the manual folder. It also contains some tips & tricks to help you be more productive when using rust-analyzer.

Security and Privacy

See the corresponding sections of the manual.

Communication

For usage and troubleshooting requests, please use "IDEs and Editors" category of the Rust forum:

https://users.rust-lang.org/c/ide/14

For questions about development and implementation, join rust-analyzer working group on Zulip:

https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Frust-analyzer

Quick Links

License

rust-analyzer is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for details.

smol_str's People

Contributors

alexheretic avatar antonsol919 avatar aochagavia avatar ark0f avatar atul9 avatar austaras avatar bors[bot] avatar computerdruid avatar dr-emann avatar dushistov avatar jd91mzm2 avatar jeffa5 avatar kestred avatar killercup avatar matklad avatar moulins avatar msdrigg avatar multisamplednight avatar novacrazy avatar robjtede avatar sinkuu avatar swatinem avatar veykril avatar waywardmonkeys avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

smol_str's Issues

Optimised str methods: `to_*case

Currently SmolStr can use str methods like to_lowercase. But they, of course, return String. In cases like short SmolStrs it would be preferable, and feasible, to be able to do this conversion without allocating.

It would also be possible for &static str & Arc<str> to return clones in the case they are already lowercase.

May also apply to str::replace, str::repeat.

error[E0658]: `while` is not allowed in a `const fn`

While running cargo install cargo-audit --force with Rust 1.44 we are getting following error in smol_str:

error[E0658]: `while` is not allowed in a `const fn`
  --> C:\Users\serg\.cargo\registry\src\github.com-1ecc6299db9ec823\smol_str-0.1.17\src\lib.rs:58:9
   |
58 | /         while i < text.len() {
59 | |             buf[i] = text.as_bytes()[i];
60 | |             i += 1
61 | |         }
   | |_________^
   |
   = note: see issue #52000 <https://github.com/rust-lang/rust/issues/52000> for more information

Is there a minimum supported version for smol_str? We really love cargo audit and we can't go Rust 1.45+ due to performance issues.

WS symbol in doc unclear

The documentation refers to a strings which are

Longer than 22 bytes, but substrings of WS (see below).

and WS is not touched upon anywhere else in the documentation – I'm assuming those are the

Strings consisting of a series of newlines,

But that's really not clear without searching the source for WS.

Present in 0.1.15 and HEAD.

serde breaks when using from_reader with SmolStr

I've made a demonstration of the failures in the form of tests here: https://github.com/bbqsrc/smol_str/commit/e9480f45b7d6e9902ac83fdcc314ce5bf8e7ad9d

All of the _reader tests fail with the messages:

---- serde_tests::test_serde_reader stdout ----
thread 'serde_tests::test_serde_reader' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: string \"Hello, World\", expected a borrowed string", line: 1, column: 14)', src/libcore/result.rs:1165:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

---- serde_tests::test_serde_hashmap_reader stdout ----
thread 'serde_tests::test_serde_hashmap_reader' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: string \"a\", expected a borrowed string", line: 1, column: 5)', src/libcore/result.rs:1165:5

---- serde_tests::test_serde_struct_reader stdout ----
thread 'serde_tests::test_serde_struct_reader' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: string \"Hello, World\", expected a borrowed string", line: 1, column: 19)', src/libcore/result.rs:1165:5

---- serde_tests::test_serde_vec_reader stdout ----
thread 'serde_tests::test_serde_vec_reader' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: string \"\", expected a borrowed string", line: 1, column: 3)', src/libcore/result.rs:1165:5

Tried on the latest stable and the following nightly to confirm:

$ rustc --version
rustc 1.39.0-nightly (66bf391c3 2019-09-23)

Clarification about layout

README.md tells sizeof(String)==sizeof(SmolStr) but that isn't accurate statement due to platform bitness. And so, sizeof(SmolStr) constantly equals 24 on every platform, but String is the triplet of pointer-sized numbers: on 32-bit platform its size is 3x4 and results in sizeof(SmolStr)≠sizeof(String).
It should be stated in the README.md or changed to smth like mem::size_of::<String>() - 2 in code.

Implement `AsRef<str>`

It would be nice if the current smol_str implemented AsRef<str>, as many functions rely on that genric bound, and it's slightly tedious to use the as_str method each time. Currently, there's already an .as_str() method, so implementing this would be quite trivial:

impl AsRef<str> for SmolStr {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

I could open a pr if you'd like.

Add feature for Rc based heap-allocated string

The use of Arc for heap-allocated strings is a great default, as it works as expected in many contexts. However for more specialized (guaranteed single-threaded) cases an Rc is sufficient. It would be great if this crate had a feature flag which enabled compiling with Rc instead of Arc, for those cases.

Minor typos

Just looking at the docs for the first time and noticing a few readability issues with the module description:

/// * `size_of::<SmolStr>() == 24 (therefor == size_of::<String>() on 64 bit platforms)

It's "therefor" instead of "therefore".

The ::<SmolStr> and ::<string> doesn't show up on the crate docs either:

image

Reading this (rust-lang/rust#18419), it sounds like you need to escape the < and < since they are HTML. Probably just &lt; and &gt;.

1.46 requirement is problematic

Due to rust-lang/rust#75992 I'm unable to update to Rust 1.46-1.48, and currently smol_str is the only crate in my dependency tree that breaks on Rust 1.45.

Could you make the new const fn feature conditional, or yank the latest release?

0.1.17 doesn't build on fairly-recent rust releases

I just stumbled over serokell/deploy-rs#27, where smol_str is pulled into a deploy tool via a transitive dependency.

I know that smol_str doesn't have a published minimum supported rust version, but it feels like the transition between 0.1.16 and 0.1.17 should have been a major version bump, being a breaking change for rust versions from this July...

I don't have any good ideas on how to fix this situation now - but maybe cutting a major release and maybe pulling or reverting the 0.1.17 release will not be too painful?

serde is not an optional dependency

Judging from how the Cargo.toml is written, I assumed that serde was supposed to be an optional dependency.
However, when inspecting cargo bloat for my project (which uses smol_str) I noticed a long compile time for serde.
I was able to reproduce with an empty project (just an empty src/lib.rs file):

[package]
name = "foo"
version = "0.1.0"
edition = "2021"
[dependencies]
smol_str = "0.1.21"

Running cargo check shows that serde is compiled and running cargo bloat yields

> cargo bloat --time -j 1
Time Crate
3.20s serde
0.39s smol_str

Just as an example for my larger project, serde is still among the top 7 contributors even tough I am not using it at all (and according to cargo tree only depend upon it trough smol_str).

> cargo bloat --time -j 1
 Time Crate
9.99s syn
9.93s hir_def
5.41s rowan
4.40s basedb
3.59s logos_derive
3.49s regex_syntax
3.10s serde
...

I am using the latest stable version (rust 1.56.1 on linux/nixos). Is there any way to remove the serde dependency or am I missing something?

Cargo failed to select version of `smol_str`.

Greetings,

I don't know where to write about this, but with 0.1.22 version, cargo sometimes refuse to build:

failed to select a version for the requirement `smol_str = "^0.1.22"`
candidate versions found which didn't match: 0.1.21, 0.1.20, 0.1.19, ...
location searched: crates.io index

But if change version on 0.1.21 (version = "=0.1.21") everything build fine.

P.S. Tried clean cargo cache and clean install.

Can `Impl From<SmolStr> for Arc<str>` be added?

This would allow easy conversion without re-allocating for longer strings. For shorter strings, allocation would be necessary of course.

Looking at the internals, it looks like it could be done with a simple match statement.

Add `SmolStrBuilder` / `SmolStrBuffer`

To build a SmolStr from parts it would be useful to have a writeable builder.

Perhaps something like

let mut result = SmolStrBuffer::new();
for part in parts {
    result.push_str(part);
}
let result = SmolStr::from(result);

This almost already exists provided by smol_str::Writer, but using this has a couple of downsides

  • Writer is hidden from docs, implying it isn't covered by stability guarantees.
  • Writer doesn't provide infallible push_str, though it trivially could.

It seems straightforward to adapt Writer to this end.

Maybe the `len` and enum branch can be merged into a `u8`?

Use 5 bit to save the length(max=31, enough for buffer), and use 2 bit to save the enum branch information(4 br). Thus the buffer size can be 23.
To implement it maybe we can no longer use enum and do transmute, the code may be a little bit ugly.

Forcing `SmolStr` to allocate on stack when the `size_hint` of an iterator is bad

Implementation

What we should do

SmolStr allocates data on heap when size_hint of a iterator is above 24.
Even when the size hint is incorrect, it keeps it's data on heap, wrapped in Arc.

I want SmolStr to allocate it's contents on stack when the resulting data is turns out to be smaller.

Why do we need this

It allows us to introduce new optimization, reduce interaction with Arc... etc.

Proof

Here is an analysis of assembly code.
(I'm pretty sure that there is a better way to do this)

You can see that my optimization reduces number of instructions and improves uOps per cycle/IPC.

Current version of the code.

https://rust.godbolt.org/z/Mh9GhYbM9

Iterations:        100
Instructions:      97400
Total Cycles:      52538
Total uOps:        124800

Dispatch Width:    6
uOps Per Cycle:    2.38
IPC:               1.85
Block RThroughput: 208.0

This one with my optimization.

https://rust.godbolt.org/z/3Gh8jacYK

Iterations:        100
Instructions:      91200
Total Cycles:      36167
Total uOps:        117500

Dispatch Width:    6
uOps Per Cycle:    3.25
IPC:               2.52
Block RThroughput: 195.8

Changelog / release notes

A breaking bump to 0.2.0 was published recently. However, I can't see any notes on why in

  • github releases / tag text
  • CHANGELOG.md file
  • README.md file

Looking at the commits it seems feature optional changes & From impls are technically breaking though should be fine for most usages. But having to read commit messages for this isn't ideal as a user of this lib.

Is there somewhere else I should be looking?

Add static string variant

Add a new variant which contains a &'static str to allow easily creating a smol str at compile time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.