Annoyed that Rust has many string types? Well it doesn't have to

Overview

generic-str

docs

The one true string type in Rust!

This project intends to be a proof-of-concept for an idea I had a few months back. There is lots of unsafe and requires nightly. Tested on cargo 1.58.0-nightly (2e2a16e98 2021-11-08)

Explanation

Rust notoriously has a few different string types. The two main contenders are:

  • &str which is a 'string reference'. It's non-resizable and it's mutability is limited.
  • String which is an 'owned string'. It's resizable and can be mutated simply.

It turns out that these two strings aren't too different. str is just a string that's backed by a [u8] byte slice. Similarly, String is just a string that's backed by a Vec<u8>.

So why are they really different types? Couldn't we theoretically have something like

type str = StringBase<[u8]>;
type String = StringBase<Vec<u8>>;

So that's what this is. It's mostly up to feature parity with the standard library strings. A lot of the standard trait implementations are there too.

I've also implemented

type str32 = StringBase<[char]>;
type String32 = StringBase<Vec<char>>;

for utf-32 applications.

generic-vec

So there was some discussion about whether Allocator was the best abstraction for customising Vec storage. I was very intrigured by this concept, and I made use of an implementation that RustyYato contributed in the thread in this project.

So, now I have

use generic_vec::{GenericVec, raw::Heap};
pub type String<A = Global> = OwnedString<u8, Heap<u8, A>>;
pub type OwnedString<U, S> = StringBase<GenericVec<U, S>>;

Which might look more complicated, and you'd be right. Implementation wise, GenericVec<U, Heap<U, A>> is supposed to be identical to Vec<u8> so it should be functionally the same as before.

But, with the added power of this storage backed system, it allows for static allocated but resizable† strings!

pub type ArrayString<const N: usize> = OwnedString<u8, UninitBuffer<[u8; N], u8>>;

And I get to re-use all of the same code from when implementing String, because it's all implemented on the base OwnedString type for string manipulations that needs resizablility.

†: obviously, they cannot be resized larger than the pre-defined N value, and it will panic in the event that you attempt to push over that.

You might also like...
Use explicit container types with Scrypto! Leverage the Rust compiler's type checking to increase security and productivity when developing Radix blueprints.

Scrypto Static Types Use explicit container types with Scrypto! Leverage the Rust compiler's type checking to increase security and productivity when

Rust types for the OASIS Common Alerting Protocol (CAP)

Rust types for the OASIS Common Alerting Protocol (CAP)

A rust program to try and detect some types of Hardware Keyloggers.
A rust program to try and detect some types of Hardware Keyloggers.

Hardware Keylogger Detection Warning: Certain Types of Hardware keyloggers can not be detected by this program, Passive Hardware Keyloggers are imposs

The most primitive and the fastest implementation of a fixed-size last-in-first-out stack on stack in Rust, for Copy-implementing types

This is the simplest and the fastest (faster than Vec!) implementation of a last-in-first-out stack data structure, on stack, when stack elements are

A list of known SS58 account types as an enum.

A list of known SS58 account types as an enum.

🪣 Types for a `Vec`'s raw parts

raw-parts A wrapper around the decomposed parts of a VecT. This struct contains the Vec's internal pointer, length, and allocated capacity. RawParts

An unsafe botched job that doesn't rely on types being 'static lifetime.

An unsafe botched job that doesn't rely on types being 'static lifetime. Will panic if provided a 0 field struct. I will fix this when I figure out how.

A repository full of manually generated hand curated JSON files, which contain the API Types that the Discord API returns.

Discord API Types A repository full of manually generated hand curated JSON files, which contain the API Types that the Discord API returns. Also did

Option and Either types with variants known at compile time.

Const Either Some types to allow deciding at compile time if an option contains a value or which variant from the either type is active. This might be

Achieve it! How you ask? Well, it's pretty simple; just use greatness!

Greatness! Achieve it! How you ask? Well, it's pretty simple; just use greatness! Disclaimer I do not believe that greatness is the best. It fits a me

Isacc Barker (Milo Banks) 107 Sep 28, 2022
Frame is a markdown language for creating state machines (automata) in 7 programming languages as well as generating UML documentation.

Frame Language Transpiler v0.5.1 Hi! So very glad you are interested in Frame. Frame system design markdown language for software architects and engin

Mark Truluck 35 Dec 31, 2022
Rust telegram bot library for many runtimes

Telbot Telbot provides telegram bot types and api wrappers. Specifically, telbot now supports: telbot-types: basic telegram types / requests / respons

kiwiyou 17 Dec 3, 2022
Might have a go at implementing Pulumi for Rust 🤷‍♂️

Might have a go at implementing Pulumi for Rust ??‍♂️

Nick 11 Nov 1, 2022
Const equivalents of many [`bytemuck`] functions, and a few additional const functions.

Const equivalents of many bytemuck functions, and a few additional const functions. constmuck uses bytemuck's traits, so any type that implements thos

null 6 Nov 4, 2021
Ointers is a library for representing pointers where some bits have been stolen so that they may be used by the programmer for something else

Ointers is a library for representing pointers where some bits have been stolen so that they may be used by the programmer for something else. In effect, it's a small amount of free storage

Irrustible 8 Jun 4, 2022
Who said python couldn't have nice errors?

potato Who said python couldn't have nice errors? Running git clone https://github.com/KittyBorgX/potato.git cd potato cargo build --release ./target/

Krishna Ramasimha 5 Jan 22, 2023
Rust library for program synthesis of string transformations from input-output examples 🔮

Synox implements program synthesis of string transformations from input-output examples. Perhaps the most well-known use of string program synthesis in end-user programs is the Flash Fill feature in Excel. These string transformations are learned from input-output examples.

Anish Athalye 21 Apr 27, 2022
A flexible, simple to use, immutable, clone-efficient String replacement for Rust

flexstr A flexible, simple to use, immutable, clone-efficient String replacement for Rust Overview Rust is great, but it's String type is optimized as

Scott Meeuwsen 119 Dec 12, 2022
Traits for inspecting memory usage of Rust types

memuse This crate contains traits for measuring the dynamic memory usage of Rust types. About Memory-tracking is a common activity in large applicatio

null 13 Dec 23, 2022