Write the value to an async writer with zero-copy optimization
(&self, writer: &mut W)
| 151 | |
| 152 | /// Write the value to an async writer with zero-copy optimization |
| 153 | pub async fn write_to<W: AsyncWrite + Unpin>(&self, writer: &mut W) -> io::Result<()> { |
| 154 | use tokio::io::AsyncWriteExt; |
| 155 | |
| 156 | match self { |
| 157 | MappedValue::Memory(data) => { |
| 158 | writer.write_all(data).await |
| 159 | } |
| 160 | MappedValue::Mapped { mmap, offset, length, .. } => { |
| 161 | let slice = &mmap[*offset..*offset + *length]; |
| 162 | writer.write_all(slice).await |
| 163 | } |
| 164 | MappedValue::Reference(data) => { |
| 165 | writer.write_all(data).await |
| 166 | } |
| 167 | MappedValue::Small(small) => { |
| 168 | // Use a stack buffer for small values |
| 169 | let mut buffer = [0u8; 32]; |
| 170 | let len = small.write_text_to_buffer(&mut buffer); |
| 171 | writer.write_all(&buffer[..len]).await |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /// Create a slice view of a portion of the data (not implemented for mapped values) |
| 177 | pub fn slice(&self, start: usize, len: usize) -> Option<MappedValue> { |