Debian Codegraph @

src/write.rs

raw symbols SCIP
1 #[cfg(feature = "alloc")]
2 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#;
3 #[cfg(not(feature = "std"))]
4 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/;
5 #[cfg(feature = "std")]
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/;
7
8 use craterust-analyzer cargo serde_cbor_2 0.13.0 crate/::errorrust-analyzer cargo serde_cbor_2 0.13.0 error/;
9
10 #[cfg(not(feature = "unsealed_read_write"))]
11 /// A sink for serialized CBOR.
12 ///
13 /// This trait is similar to the [`Write`]() trait in the standard library,
14 /// but has a smaller and more general API.
15 ///
16 /// Any object implementing `std::io::Write`
17 /// can be wrapped in an [`IoWrite`](../write/struct.IoWrite.html) that implements
18 /// this trait for the underlying object.
19 pub trait Write: private::Sealed {
20 /// The type of error returned when a write operation fails.
21 #[doc(hidden)]
22 type Error: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<errorrust-analyzer cargo serde_cbor_2 0.13.0 error/::Errorrust-analyzer cargo serde_cbor_2 0.13.0 error/Error#>;
23
24 /// Attempts to write an entire buffer into this write.
25 #[doc(hidden)]
26 fn write_all(&mut self, buf: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Self::Error>;
27 }
28
29 #[cfg(feature = "unsealed_read_write")]
30 /// A sink for serialized CBOR.
31 ///
32 /// This trait is similar to the [`Write`]() trait in the standard library,
33 /// but has a smaller and more general API.
34 ///
35 /// Any object implementing `std::io::Write`
36 /// can be wrapped in an [`IoWrite`](../write/struct.IoWrite.html) that implements
37 /// this trait for the underlying object.
38 ///
39 /// This trait is sealed by default, enabling the `unsealed_read_write` feature removes this bound
40 /// to allow objects outside of this crate to implement this trait.
41 pub trait Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write# {
42 /// The type of error returned when a write operation fails.
43 type Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/Write#Error#: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<errorrust-analyzer cargo serde_cbor_2 0.13.0 error/::Errorrust-analyzer cargo serde_cbor_2 0.13.0 error/Error#>;
44
45 /// Attempts to write an entire buffer into this write.
46 fn write_allrust-analyzer cargo serde_cbor_2 0.13.0 write/Write#write_all().(&mut self, buf: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Self::Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/Write#Error#>;
47 }
48
49 #[cfg(not(feature = "unsealed_read_write"))]
50 mod private {
51 pub trait Sealed {}
52 }
53
54 impl<W> Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write# for &mut W
55 where
56 W: Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write#,
57 {
58 type Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`&mut W`][Write]Error# = W::Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/Write#Error#;
59
60 fn write_allrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`&mut W`][Write]write_all().(&mut self, buf: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Selfrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`&mut W`][Write]::Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/Write#Error#> {
61 (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self).write_allrust-analyzer cargo serde_cbor_2 0.13.0 write/Write#write_all().(buf)
62 }
63 }
64
65 #[cfg(not(feature = "unsealed_read_write"))]
66 impl<W> private::Sealed for &mut W where W: Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write# {}
67
68 #[cfg(feature = "std")]
69 /// A wrapper for types that implement
70 /// [`std::io::Write`](https://doc.rust-lang.org/std/io/trait.Write.html) to implement the local
71 /// [`Write`](trait.Write.html) trait.
72 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
73 pub struct IoWriterust-analyzer cargo serde_cbor_2 0.13.0 write/IoWrite#<W>(W);
74
75 #[cfg(feature = "std")]
76 impl<W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#> IoWriterust-analyzer cargo serde_cbor_2 0.13.0 write/IoWrite#<W> {
77 /// Wraps an `io::Write` writer to make it compatible with [`Write`](trait.Write.html)
78 pub fn newrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`IoWrite<W>`]new().(w: W) -> IoWriterust-analyzer cargo serde_cbor_2 0.13.0 write/IoWrite#<W> {
79 IoWriterust-analyzer cargo serde_cbor_2 0.13.0 write/IoWrite#(w)
80 }
81 }
82
83 #[cfg(feature = "std")]
84 impl<W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#> Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write# for IoWriterust-analyzer cargo serde_cbor_2 0.13.0 write/IoWrite#<W> {
85 type Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`IoWrite<W>`][Write]Error# = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#;
86
87 fn write_allrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`IoWrite<W>`][Write]write_all().(&mut self, buf: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Selfrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`IoWrite<W>`][Write]::Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/Write#Error#> {
88 self.0rust-analyzer cargo serde_cbor_2 0.13.0 write/IoWrite#0..write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(buf)
89 }
90 }
91
92 #[cfg(all(feature = "std", not(feature = "unsealed_read_write")))]
93 impl<W> private::Sealed for IoWriterust-analyzer cargo serde_cbor_2 0.13.0 write/IoWrite#<W> where W: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# {}
94
95 #[cfg(any(feature = "std", feature = "alloc"))]
96 impl Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write# for Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> {
97 type Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`Vec<u8>`][Write]Error# = errorrust-analyzer cargo serde_cbor_2 0.13.0 error/::Errorrust-analyzer cargo serde_cbor_2 0.13.0 error/Error#;
98
99 fn write_allrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`Vec<u8>`][Write]write_all().(&mut self, buf: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Selfrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`Vec<u8>`][Write]::Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/Write#Error#> {
100 self.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(buf);
101 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
102 }
103 }
104
105 #[cfg(all(
106 any(feature = "std", feature = "alloc"),
107 not(feature = "unsealed_read_write")
108 ))]
109 impl private::Sealed for Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> {}
110
111 #[cfg(not(feature = "std"))]
112 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debug)]
113 pub struct FmtWrite<'a, W: Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write#>(&'a mut W);
114
115 #[cfg(not(feature = "std"))]
116 impl<'a, W: Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write#> FmtWrite<'a, W> {
117 /// Wraps an `fmt::Write` writer to make it compatible with [`Write`](trait.Write.html)
118 pub fn new(w: &'a mut W) -> FmtWrite<'a, W> {
119 FmtWrite(w)
120 }
121 }
122
123 #[cfg(not(feature = "std"))]
124 impl<'a, W: Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write#> fmt::Write for FmtWrite<'a, W> {
125 fn write_str(&mut self, s: &str) -> fmt::Result {
126 selfrust-analyzer cargo serde_cbor_2 0.13.0 write/.0.write_all(s.as_bytes()).map_err(|_| fmt::Error)
127 }
128 }
129
130 #[cfg(all(not(feature = "std"), not(feature = "unsealed_read_write")))]
131 impl<'a, W> private::Sealed for FmtWrite<'a, W> where W: Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write# {}
132
133 /// Implements [`Write`](trait.Write.html) for mutable byte slices (`&mut [u8]`).
134 ///
135 /// Returns an error if the value to serialize is too large to fit in the slice.
136 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
137 pub struct SliceWriterust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#<'a> {
138 slicerust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#slice.: &'a mut [u8],
139 indexrust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#index.: usize,
140 }
141
142 impl<'a> SliceWriterust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#<'a> {
143 /// Wraps a mutable slice so it can be used as a `Write`.
144 pub fn newrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`SliceWrite<'a>`]new().(slice: &'a mut [u8]) -> SliceWriterust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#<'a> {
145 SliceWriterust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite# { slicerust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#slice., indexrust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#index.: 0 }
146 }
147
148 /// Returns the number of bytes written to the underlying slice.
149 pub fn bytes_writtenrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`SliceWrite<'a>`]bytes_written().(&self) -> usize {
150 self.indexrust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#index.
151 }
152
153 /// Returns the underlying slice.
154 pub fn into_innerrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`SliceWrite<'a>`]into_inner().(self) -> &'a mut [u8] {
155 self.slicerust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#slice.
156 }
157 }
158
159 impl Writerust-analyzer cargo serde_cbor_2 0.13.0 write/Write# for SliceWriterust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#<'_> {
160 type Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`SliceWrite<'_>`][Write]Error# = errorrust-analyzer cargo serde_cbor_2 0.13.0 error/::Errorrust-analyzer cargo serde_cbor_2 0.13.0 error/Error#;
161
162 fn write_allrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`SliceWrite<'_>`][Write]write_all().(&mut self, buf: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Selfrust-analyzer cargo serde_cbor_2 0.13.0 write/impl#[`SliceWrite<'_>`][Write]::Errorrust-analyzer cargo serde_cbor_2 0.13.0 write/Write#Error#> {
163 if self.slicerust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#slice..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().self.indexrust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#index. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
164 // This buffer will not fit in our slice
165 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(errorrust-analyzer cargo serde_cbor_2 0.13.0 error/::Errorrust-analyzer cargo serde_cbor_2 0.13.0 error/Error#::scratch_too_smallrust-analyzer cargo serde_cbor_2 0.13.0 error/impl#[Error]scratch_too_small().(self.indexrust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#index. as u64));
166 }
167 let end = self.indexrust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#index. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().();
168 self.slicerust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#slice.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().self.indexrust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#index...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(buf);
169 self.indexrust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#index. = end;
170 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
171 }
172 }
173
174 #[cfg(not(feature = "unsealed_read_write"))]
175 impl private::Sealed for SliceWriterust-analyzer cargo serde_cbor_2 0.13.0 write/SliceWrite#<'_> {}