pub struct Dec<'a> { /* private fields */ }Expand description
Bounds-checked reader. Every accessor returns None rather than panicking so a malformed reply from the remote cannot take the daemon down.
Implementations§
Source§impl<'a> Dec<'a>
impl<'a> Dec<'a>
Sourcepub fn new(b: &'a [u8]) -> Self
pub fn new(b: &'a [u8]) -> Self
Examples found in repository?
examples/measure-roundtrips.rs (line 168)
157async fn list(s: &mut Session, dir: &str) -> Result<Vec<(String, Attrs)>> {
158 let id = s.alloc_id();
159 s.queue(OPENDIR, &Enc::new().u32(id).str(dir.as_bytes()).done())
160 .await?;
161 s.flush().await?;
162 let r = s.recv().await?;
163 ensure!(
164 r.kind == HANDLE,
165 "opendir {dir} refused (reply type {})",
166 r.kind
167 );
168 let handle = Dec::new(r.payload())
169 .str()
170 .context("opendir handle")?
171 .to_vec();
172
173 let mut out = Vec::new();
174 loop {
175 let id = s.alloc_id();
176 s.queue(READDIR, &Enc::new().u32(id).str(&handle).done())
177 .await?;
178 s.flush().await?;
179 let r = s.recv().await?;
180 if r.kind == STATUS {
181 break;
182 }
183 ensure!(r.kind == NAME, "readdir gave reply type {}", r.kind);
184 let mut d = Dec::new(r.payload());
185 let count = d.u32().context("readdir count")?;
186 for _ in 0..count {
187 let name = String::from_utf8_lossy(d.str().context("filename")?).into_owned();
188 d.str().context("longname")?;
189 let attrs = Attrs::decode(&mut d).context("attrs")?;
190 out.push((name, attrs));
191 }
192 }
193
194 let id = s.alloc_id();
195 s.queue(CLOSE, &Enc::new().u32(id).str(&handle).done())
196 .await?;
197 s.flush().await?;
198 s.recv().await?;
199 Ok(out)
200}
201
202async fn batch_open(s: &mut Session, paths: &[String]) -> Result<(Duration, Vec<Vec<u8>>)> {
203 let t = Instant::now();
204 for p in paths {
205 let id = s.alloc_id();
206 s.queue(
207 OPEN,
208 &Enc::new()
209 .u32(id)
210 .str(p.as_bytes())
211 .u32(FXF_READ)
212 .u32(0)
213 .done(),
214 )
215 .await?;
216 }
217 s.flush().await?;
218
219 let mut handles = Vec::with_capacity(paths.len());
220 for _ in 0..paths.len() {
221 let r = s.recv().await?;
222 ensure!(r.kind == HANDLE, "open refused (reply type {})", r.kind);
223 handles.push(Dec::new(r.payload()).str().context("open handle")?.to_vec());
224 }
225 Ok((t.elapsed(), handles))
226}
227
228async fn batch_read(s: &mut Session, handles: &[Vec<u8>]) -> Result<(Duration, usize)> {
229 let t = Instant::now();
230 for h in handles {
231 let id = s.alloc_id();
232 s.queue(READ, &Enc::new().u32(id).str(h).u64(0).u32(READ_LEN).done())
233 .await?;
234 }
235 s.flush().await?;
236
237 let mut bytes = 0usize;
238 for _ in 0..handles.len() {
239 let r = s.recv().await?;
240 match r.kind {
241 DATA => bytes += Dec::new(r.payload()).str().map_or(0, |b| b.len()),
242 STATUS => {}
243 other => bail!("read gave reply type {other}"),
244 }
245 }
246 Ok((t.elapsed(), bytes))
247}Sourcepub fn u32(&mut self) -> Option<u32>
pub fn u32(&mut self) -> Option<u32>
Examples found in repository?
examples/measure-roundtrips.rs (line 185)
157async fn list(s: &mut Session, dir: &str) -> Result<Vec<(String, Attrs)>> {
158 let id = s.alloc_id();
159 s.queue(OPENDIR, &Enc::new().u32(id).str(dir.as_bytes()).done())
160 .await?;
161 s.flush().await?;
162 let r = s.recv().await?;
163 ensure!(
164 r.kind == HANDLE,
165 "opendir {dir} refused (reply type {})",
166 r.kind
167 );
168 let handle = Dec::new(r.payload())
169 .str()
170 .context("opendir handle")?
171 .to_vec();
172
173 let mut out = Vec::new();
174 loop {
175 let id = s.alloc_id();
176 s.queue(READDIR, &Enc::new().u32(id).str(&handle).done())
177 .await?;
178 s.flush().await?;
179 let r = s.recv().await?;
180 if r.kind == STATUS {
181 break;
182 }
183 ensure!(r.kind == NAME, "readdir gave reply type {}", r.kind);
184 let mut d = Dec::new(r.payload());
185 let count = d.u32().context("readdir count")?;
186 for _ in 0..count {
187 let name = String::from_utf8_lossy(d.str().context("filename")?).into_owned();
188 d.str().context("longname")?;
189 let attrs = Attrs::decode(&mut d).context("attrs")?;
190 out.push((name, attrs));
191 }
192 }
193
194 let id = s.alloc_id();
195 s.queue(CLOSE, &Enc::new().u32(id).str(&handle).done())
196 .await?;
197 s.flush().await?;
198 s.recv().await?;
199 Ok(out)
200}pub fn u64(&mut self) -> Option<u64>
Sourcepub fn str(&mut self) -> Option<&'a [u8]>
pub fn str(&mut self) -> Option<&'a [u8]>
Examples found in repository?
examples/measure-roundtrips.rs (line 169)
157async fn list(s: &mut Session, dir: &str) -> Result<Vec<(String, Attrs)>> {
158 let id = s.alloc_id();
159 s.queue(OPENDIR, &Enc::new().u32(id).str(dir.as_bytes()).done())
160 .await?;
161 s.flush().await?;
162 let r = s.recv().await?;
163 ensure!(
164 r.kind == HANDLE,
165 "opendir {dir} refused (reply type {})",
166 r.kind
167 );
168 let handle = Dec::new(r.payload())
169 .str()
170 .context("opendir handle")?
171 .to_vec();
172
173 let mut out = Vec::new();
174 loop {
175 let id = s.alloc_id();
176 s.queue(READDIR, &Enc::new().u32(id).str(&handle).done())
177 .await?;
178 s.flush().await?;
179 let r = s.recv().await?;
180 if r.kind == STATUS {
181 break;
182 }
183 ensure!(r.kind == NAME, "readdir gave reply type {}", r.kind);
184 let mut d = Dec::new(r.payload());
185 let count = d.u32().context("readdir count")?;
186 for _ in 0..count {
187 let name = String::from_utf8_lossy(d.str().context("filename")?).into_owned();
188 d.str().context("longname")?;
189 let attrs = Attrs::decode(&mut d).context("attrs")?;
190 out.push((name, attrs));
191 }
192 }
193
194 let id = s.alloc_id();
195 s.queue(CLOSE, &Enc::new().u32(id).str(&handle).done())
196 .await?;
197 s.flush().await?;
198 s.recv().await?;
199 Ok(out)
200}
201
202async fn batch_open(s: &mut Session, paths: &[String]) -> Result<(Duration, Vec<Vec<u8>>)> {
203 let t = Instant::now();
204 for p in paths {
205 let id = s.alloc_id();
206 s.queue(
207 OPEN,
208 &Enc::new()
209 .u32(id)
210 .str(p.as_bytes())
211 .u32(FXF_READ)
212 .u32(0)
213 .done(),
214 )
215 .await?;
216 }
217 s.flush().await?;
218
219 let mut handles = Vec::with_capacity(paths.len());
220 for _ in 0..paths.len() {
221 let r = s.recv().await?;
222 ensure!(r.kind == HANDLE, "open refused (reply type {})", r.kind);
223 handles.push(Dec::new(r.payload()).str().context("open handle")?.to_vec());
224 }
225 Ok((t.elapsed(), handles))
226}
227
228async fn batch_read(s: &mut Session, handles: &[Vec<u8>]) -> Result<(Duration, usize)> {
229 let t = Instant::now();
230 for h in handles {
231 let id = s.alloc_id();
232 s.queue(READ, &Enc::new().u32(id).str(h).u64(0).u32(READ_LEN).done())
233 .await?;
234 }
235 s.flush().await?;
236
237 let mut bytes = 0usize;
238 for _ in 0..handles.len() {
239 let r = s.recv().await?;
240 match r.kind {
241 DATA => bytes += Dec::new(r.payload()).str().map_or(0, |b| b.len()),
242 STATUS => {}
243 other => bail!("read gave reply type {other}"),
244 }
245 }
246 Ok((t.elapsed(), bytes))
247}Auto Trait Implementations§
impl<'a> Freeze for Dec<'a>
impl<'a> RefUnwindSafe for Dec<'a>
impl<'a> Send for Dec<'a>
impl<'a> Sync for Dec<'a>
impl<'a> Unpin for Dec<'a>
impl<'a> UnsafeUnpin for Dec<'a>
impl<'a> UnwindSafe for Dec<'a>
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more