#string #immutability #persistent #cons #prepend

nightly char-list

具有与字符链表相同API的持久化字符串类型

7个不稳定版本 (3个破坏性版本)

0.4.0 2024年3月23日
0.2.1 2023年8月22日
0.1.0 2022年11月5日
0.0.3 2022年10月26日

#1568数据结构

每月28次下载

MIT 许可证

79KB
1K SLoC

char-list

具有与字符链表相同API的持久化字符串类型。

目录

FiniteCharList

FiniteCharList 类型可能是您想要使用的。它的行为就像一个字符链表,但性能更好(待验证)。

CharList<>

CharList<Tail> 类型允许通过允许自定义尾来自定义数据结构。我正在使用这个来模拟Prolog的开放式列表,其中列表的尾可能是一个未实例化的Prolog变量。

FiniteCharList 只是一个类型别名,对应于 CharList<()>

泛型 CharList 由于尾部性质未知,因此提供的功能较少。例如,CharList<Tail> 并未实现 AsRef<str> 或甚至 PartialEq

目的是您将一个 CharList<YourTail> 包裹在一个新类型中,并在您的问题空间中实现这些特性。例如,我(计划)使用一个 CharList<ast::Expression>,它将实现 PartialEq,因为我想要语法上的等价。

文档

有关内存布局 图解 和整个工作原理的解释,请参阅文档

免责声明: unsafe

这个包还在开发中。具体来说,并非所有使用 unsafe 的地方都经过了验证! 请暂时不要用它做任何重要的事情。

欢迎并感谢安全审计!我写 unsafe 代码还是个新手。

此外,这个包依赖于 front-vec,它也非常需要审计。

示例

use assert2::assert; // For nicer assertions.
let icon = FiniteCharList::from("icon");
let nomicon = icon.cons_str("nom");
let rustonomicon = nomicon.cons_str("rusto");

// Cloning requires
//     1) copying two words, and
//     2) performing one BTreeMap lookup.
let rustonomicon2 = rustonomicon.clone();

assert!(icon == "icon");
assert!(nomicon == "nomicon");
assert!(rustonomicon == "rustonomicon");
assert!(rustonomicon == rustonomicon2);

// No new allocations required (if underlying buffer capacity allows).
// Mutably prepends the `&str` to the buffer without shifting memory.
let the_book = rustonomicon.cons_str("the ");
assert!(the_book == "the rustonomicon");

// Drop to mutably resize underlying buffer and 
drop(rustonomicon);
drop(the_book);
drop(rustonomicon2);

let janelle_monae = icon.cons('b'); // Duplication required now.
assert!(janelle_monae == "bicon");

依赖项

~2MB
~45K SLoC