helix_core/effect/sink.rs
1use super::Effect;
2
3// ─── EffectSink ──────────────────────────────────────────────────────────────
4
5/// Effect 输出缓冲(写入端)。
6///
7/// 由 `ExecutionShell` 持有,跨 `step()` 调用复用(`clear` 而非 `drop+new`),
8/// 实现热路径零分配(轴①)。
9///
10/// 模块通过 `&mut EffectSink` 参数写入 effect,driver 通过 `as_slice()` 读取。
11/// 原草稿中 `EffectBuffer` / `EffectBatch<'_>` / `EffectSink` 三个名字统一为此类型
12/// (审查意见2 S4 落实)。
13pub struct EffectSink {
14 buf: Vec<Effect>,
15}
16
17impl EffectSink {
18 pub fn new() -> Self {
19 Self {
20 buf: Vec::with_capacity(8),
21 }
22 }
23
24 /// 写入一个 Effect
25 #[inline]
26 pub fn push(&mut self, e: Effect) {
27 self.buf.push(e);
28 }
29
30 /// 清空缓冲(跨 step 复用,不释放内存)
31 #[inline]
32 pub fn clear(&mut self) {
33 self.buf.clear();
34 }
35
36 /// 读取本次 step 产出的全部 Effect
37 #[inline]
38 pub fn as_slice(&self) -> &[Effect] {
39 &self.buf
40 }
41
42 /// 是否有 Effect 产出
43 #[inline]
44 pub fn is_empty(&self) -> bool {
45 self.buf.is_empty()
46 }
47}
48
49impl Default for EffectSink {
50 fn default() -> Self {
51 Self::new()
52 }
53}