2次发布
0.1.1 | 2023年3月23日 |
---|---|
0.1.0 | 2023年3月23日 |
#2280 在 算法
每月24次下载
14KB
185 行
在许多日志场景中,通常需要限制日志的最大大小,同时只保留最新的内容。一种常见的方法是使用固定大小的环形缓冲区,通过删除旧内容来为新内容腾出空间。
此crate支持一种类型,StringRing
,它是一个UTF-8编码字符串的环形缓冲区。支持多种Granularity
模式,适用于各种应用,用于控制如何删除旧内容。
示例
以下是一个基本的Granularity::Character
模式示例
# use string_ring::*;
// example of a 16-byte circular string buffer
let mut buf = StringRing::new(16, Granularity::Character);
buf.push("hello world!");
assert_eq!(buf.make_contiguous(), "hello world!");
buf.push("more content!");
assert_eq!(buf.make_contiguous(), "ld!more content!");
以下是一个Granularity::Line
模式示例,这对于日志记录通常更有用
# use string_ring::*;
// example of a 26-byte circular string buffer
let mut buf = StringRing::new(26, Granularity::Line);
buf.push("hello world!\n");
assert_eq!(buf.make_contiguous(), "hello world!\n");
buf.push("more!\n");
assert_eq!(buf.make_contiguous(), "hello world!\nmore!\n");
buf.push("too much!\n");
assert_eq!(buf.make_contiguous(), "more!\ntoo much!\n");
no-std
此crate通过禁用默认功能支持在no-std
环境中构建
[dependencies]
string-ring = { version = "...", use-default-features = false }
请注意,在此情况下仍然需要alloc
crate,因为目前没有StringRing
的无堆版本。
依赖项
~190–335KB