|
| 1 | +// ... (imports remain) |
| 2 | +use crate::message::AsMut; |
| 3 | +use crate::Status; |
| 4 | +use send_future::SendFuture; |
| 5 | + |
| 6 | +#[trait_variant::make(Send)] |
| 7 | +pub trait Lazy<Req>: Send |
| 8 | +where |
| 9 | + Req: AsMut, |
| 10 | +{ |
| 11 | + async fn resolve(self, target: <Req as AsMut>::Mut<'_>) -> Result<(), Status>; |
| 12 | +} |
| 13 | + |
| 14 | +#[trait_variant::make(Send)] |
| 15 | +pub trait Mapper<Req>: Send |
| 16 | +where |
| 17 | + Req: AsMut, |
| 18 | +{ |
| 19 | + async fn map(self, target: <Req as AsMut>::Mut<'_>) -> Result<(), Status>; |
| 20 | +} |
| 21 | + |
| 22 | +// --- Zero-Cost Map Combinator --- |
| 23 | + |
| 24 | +pub struct MapLazy<L, M> { |
| 25 | + pub inner: L, |
| 26 | + pub mapper: M, |
| 27 | +} |
| 28 | + |
| 29 | +impl<Req, L, M> Lazy<Req> for MapLazy<L, M> |
| 30 | +where |
| 31 | + Req: AsMut, |
| 32 | + L: Lazy<Req>, |
| 33 | + M: Mapper<Req>, |
| 34 | +{ |
| 35 | + async fn resolve(self, mut target: <Req as AsMut>::Mut<'_>) -> Result<(), Status> { |
| 36 | + let inner_target = <Req as AsMut>::reborrow_view(&mut target); |
| 37 | + self.inner.resolve(inner_target).send().await?; |
| 38 | + self.mapper.map(target).send().await |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// --- Sync Map Combinator (Sugar) --- |
| 43 | + |
| 44 | +pub struct SyncMapLazy<L, F> { |
| 45 | + pub inner: L, |
| 46 | + pub f: F, |
| 47 | +} |
| 48 | + |
| 49 | +impl<Req, L, F> Lazy<Req> for SyncMapLazy<L, F> |
| 50 | +where |
| 51 | + Req: AsMut, |
| 52 | + L: Lazy<Req>, |
| 53 | + F: FnOnce(<Req as AsMut>::Mut<'_>) -> Result<(), Status> + Send, |
| 54 | +{ |
| 55 | + async fn resolve(self, mut target: <Req as AsMut>::Mut<'_>) -> Result<(), Status> { |
| 56 | + let inner_target = <Req as AsMut>::reborrow_view(&mut target); |
| 57 | + self.inner.resolve(inner_target).send().await?; |
| 58 | + (self.f)(target) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +pub trait LazyExt<Req>: Lazy<Req> + Sized |
| 63 | +where |
| 64 | + Req: AsMut, |
| 65 | +{ |
| 66 | + fn then<M>(self, mapper: M) -> MapLazy<Self, M> { |
| 67 | + MapLazy { |
| 68 | + inner: self, |
| 69 | + mapper, |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Sugar for synchronous closures |
| 74 | + fn map<F>(self, f: F) -> SyncMapLazy<Self, F> |
| 75 | + where |
| 76 | + F: FnOnce(<Req as AsMut>::Mut<'_>) -> Result<(), Status> + Send, |
| 77 | + { |
| 78 | + SyncMapLazy { inner: self, f } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<Req, L: Lazy<Req>> LazyExt<Req> for L where Req: AsMut {} |
| 83 | + |
| 84 | +// --- Test Usage --- |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use super::*; |
| 89 | + use protobuf_well_known_types::Timestamp; |
| 90 | + |
| 91 | + // A struct for our lazy logic (Zero Cost) |
| 92 | + struct SetSeconds { |
| 93 | + value: i64, |
| 94 | + } |
| 95 | + |
| 96 | + impl Lazy<Timestamp> for SetSeconds { |
| 97 | + async fn resolve(self, mut target: <Timestamp as AsMut>::Mut<'_>) -> Result<(), Status> { |
| 98 | + target.set_seconds(self.value); |
| 99 | + Ok(()) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // A struct for our mapping logic (Zero Cost) |
| 104 | + struct AddFive; |
| 105 | + impl Mapper<Timestamp> for AddFive { |
| 106 | + async fn map(self, mut target: <Timestamp as AsMut>::Mut<'_>) -> Result<(), Status> { |
| 107 | + let current = target.seconds(); |
| 108 | + target.set_seconds(current + 5); |
| 109 | + Ok(()) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + #[tokio::test] |
| 114 | + async fn test_zero_allocation_chain() { |
| 115 | + // 1. Create the base lazy (Struct, not closure) |
| 116 | + let lazy = SetSeconds { value: 10 }; |
| 117 | + |
| 118 | + // 2. Map it with a struct mapper |
| 119 | + let chained = lazy.then(AddFive); |
| 120 | + |
| 121 | + let mut msg = Timestamp::new(); |
| 122 | + |
| 123 | + chained.resolve(msg.as_mut()).await.unwrap(); |
| 124 | + |
| 125 | + assert_eq!(msg.seconds(), 15); |
| 126 | + } |
| 127 | + |
| 128 | + #[tokio::test] |
| 129 | + async fn test_sync_closure_sugar() { |
| 130 | + // 1. Create the base lazy |
| 131 | + let lazy = SetSeconds { value: 10 }; |
| 132 | + |
| 133 | + // 2. Map it with a sync closure (Sugar) |
| 134 | + let chained = lazy.map(|mut target| { |
| 135 | + let current = target.seconds(); |
| 136 | + target.set_seconds(current + 20); |
| 137 | + Ok(()) |
| 138 | + }); |
| 139 | + |
| 140 | + let mut msg = Timestamp::new(); |
| 141 | + // ZeroBox allocation! Sync closures are easy. |
| 142 | + chained.resolve(msg.as_mut()).await.unwrap(); |
| 143 | + |
| 144 | + assert_eq!(msg.seconds(), 30); |
| 145 | + } |
| 146 | +} |
0 commit comments