(&mut self, buf: &[u8])
| 100 | |
| 101 | impl io::Write for Segment { |
| 102 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 103 | let mut storage = self.storage.write().unwrap(); |
| 104 | |
| 105 | let mut remaining = (storage.alloc - self.pos) as usize; |
| 106 | // If we don't have enough space, allocate some. |
| 107 | // If not enough space to write all of `buf` can be allocated, |
| 108 | // just write as much as we can. The next `write` call will return |
| 109 | // ENOSPC then. |
| 110 | if remaining == 0 { |
| 111 | let mut avail = self.space.lock().unwrap(); |
| 112 | if *avail == 0 { |
| 113 | return Err(enospc()); |
| 114 | } |
| 115 | |
| 116 | let want = (buf.len() - remaining).next_multiple_of(PAGE_SIZE); |
| 117 | let have = want.min(*avail as usize); |
| 118 | |
| 119 | storage.alloc += have as u64; |
| 120 | *avail -= have as u64; |
| 121 | remaining = (storage.alloc - self.pos) as usize; |
| 122 | } |
| 123 | |
| 124 | let read = buf.len().min(remaining); |
| 125 | storage.buf.extend(&buf[..read]); |
| 126 | self.pos += read as u64; |
| 127 | |
| 128 | Ok(read) |
| 129 | } |
| 130 | |
| 131 | fn flush(&mut self) -> io::Result<()> { |
| 132 | Ok(()) |
no test coverage detected