1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use core::result;
use core::ops::{Index, IndexMut, RangeFrom};
use crate::ctx::{TryIntoCtx, MeasureWith};
use crate::error;
pub trait Pwrite<Ctx, E> : Index<usize> + IndexMut<RangeFrom<usize>> + MeasureWith<Ctx>
where
Ctx: Copy,
E: From<error::Error>,
{
fn pwrite<N: TryIntoCtx<Ctx, <Self as Index<RangeFrom<usize>>>::Output, Error = E>>(&mut self, n: N, offset: usize) -> result::Result<usize, E> where Ctx: Default {
self.pwrite_with(n, offset, Ctx::default())
}
fn pwrite_with<N: TryIntoCtx<Ctx, <Self as Index<RangeFrom<usize>>>::Output, Error = E>>(&mut self, n: N, offset: usize, ctx: Ctx) -> result::Result<usize, E> {
let len = self.measure_with(&ctx);
if offset >= len {
return Err(error::Error::BadOffset(offset).into())
}
let dst = &mut self[offset..];
n.try_into_ctx(dst, ctx)
}
#[inline]
fn gwrite<N: TryIntoCtx<Ctx, <Self as Index<RangeFrom<usize>>>::Output, Error = E>>(&mut self, n: N, offset: &mut usize) -> result::Result<usize, E> where
Ctx: Default {
let ctx = Ctx::default();
self.gwrite_with(n, offset, ctx)
}
#[inline]
fn gwrite_with<N: TryIntoCtx<Ctx, <Self as Index<RangeFrom<usize>>>::Output, Error = E>>(&mut self, n: N, offset: &mut usize, ctx: Ctx) -> result::Result<usize, E> {
let o = *offset;
match self.pwrite_with(n, o, ctx) {
Ok(size) => {
*offset += size;
Ok(size)
},
err => err
}
}
}
impl<Ctx: Copy,
E: From<error::Error>,
R: ?Sized + Index<usize> + IndexMut<RangeFrom<usize>> + MeasureWith<Ctx>>
Pwrite<Ctx, E> for R {}