#strings #reference-counting #cstr #immutability

arccstr

Thread-safe, reference-counted null-terminated immutable strings

21 releases (12 stable)

1.3.1 May 29, 2021
1.3.0 Apr 6, 2020
1.2.3 Dec 4, 2018
1.2.2 Oct 31, 2018
0.3.2 Mar 21, 2017

#35 in Concurrency

Download history 297/week @ 2021-06-29 334/week @ 2021-07-06 453/week @ 2021-07-13 726/week @ 2021-07-20 585/week @ 2021-07-27 487/week @ 2021-08-03 761/week @ 2021-08-10 1007/week @ 2021-08-17 1909/week @ 2021-08-24 2287/week @ 2021-08-31 2619/week @ 2021-09-07 2365/week @ 2021-09-14 2442/week @ 2021-09-21 2855/week @ 2021-09-28 1381/week @ 2021-10-05 47/week @ 2021-10-12

2,112 downloads per month
Used in 5 crates (2 directly)

MIT/Apache

31KB
380 lines

Crates.io Documentation Build Status Codecov

Thread-safe reference-counted null-terminated strings.

This crate provides a space efficient mechanism for storing immutable strings. The best illustration of this is to go over the alternatives:

// &str:
//  - content must be known at compile time
//  + can be shared between threads
//  + space overhead is 2*usize (fat pointer to the string)
let s = "foobar";
// String
//  + can be created at runtime
//  - cannot be shared between threads (except with Clone)
//  - space overhead of 3*usize (Vec capacity and len + pointer to bytes)
//  - accessing string requires two pointer derefs
let s = format!("foobar");
// CString:
//  * mostly same as String
//  * space overhead is 2*usize (uses Box<[u8]> internally)
//  - cannot contain internal \0 bytes
use std::ffi::CString;
let s = CString::new("foobar").unwrap();
// CStr:
//  + space overhead is just the pointer (1*usize)
//  - hard to construct
//  - cannot contain internal \0 bytes
//  - generally cannot be shared between threds (lifetime usually not 'static)
use std::ffi::CStr;
let s: &CStr = &*s;
// Arc<String>:
//  + can be created at runtime
//  + can be shared between threads
//  - space overhead is 7*usize:
//     - pointer to Arc
//     - weak count
//     - strong count
//     - pointer to String
//     - String overhead (3*usize)
use std::sync::Arc;
let s = ArcCStr::try_from(format!("foobar")).unwrap();
// Arc<str>:
//  + can be created at runtime
//  + can be shared between threads
//  - space overhead is 4*usize:
//     - pointer to Arc
//     - str length
//     - weak count
//     - strong count
let s: Arc<str> = Arc::from("foobar");
// Arc<CStr>:
//  + can be created at runtime
//  + can be shared between threads
//  - space overhead is 4*usize:
//     - pointer to Arc
//     - CStr length
//     - weak count
//     - strong count
//  - cannot contain internal \0 bytes
let s: Arc<CStr> = Arc::from(CStr::from_bytes_with_nul(b"foobar\0").unwrap());
// ArcCStr:
//  + can be created at runtime
//  + can be shared between threads
//  - space overhead is 2*usize (pointer + strong count)
//  - cannot contain internal \0 bytes
use arccstr::ArcCStr;
let s = ArcCStr::try_from("foobar").unwrap();

See the ArcCStr documentation for more details.

Dependencies

~145–260KB