| 1 | #[cfg(not(feature = "std"))] |
| 2 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::{ |
| 3 | borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::ToOwnedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/ToOwned#, |
| 4 | collectionsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/::BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#, |
| 5 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!, |
| 6 | stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::{Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, ToStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/ToString#}, |
| 7 | 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#, |
| 8 | }; |
| 9 | #[cfg(feature = "std")] |
| 10 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#, 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#}; |
| 11 | |
| 12 | use craterust-analyzer cargo bendy 0.6.1 crate/::{ |
| 13 | encodingrust-analyzer cargo bendy 0.6.1 encoding/::{Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#, PrintableIntegerrust-analyzer cargo bendy 0.6.1 encoding/printable_integer/PrintableInteger#, ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#}, |
| 14 | state_trackerrust-analyzer cargo bendy 0.6.1 state_tracker/::{StateTrackerrust-analyzer cargo bendy 0.6.1 state_tracker/state/StateTracker#, StructureErrorrust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#, Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#}, |
| 15 | }; |
| 16 | |
| 17 | /// The actual encoder. Unlike the decoder, this is not zero-copy, as that would |
| 18 | /// result in a horrible interface |
| 19 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 20 | pub struct Encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder# { |
| 21 | staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state.: StateTrackerrust-analyzer cargo bendy 0.6.1 state_tracker/state/StateTracker#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 22 | outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 23 | } |
| 24 | |
| 25 | impl Encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder# { |
| 26 | /// Create a new encoder |
| 27 | pub fn newrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]new().() -> Selfrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder] { |
| 28 | <Selfrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder] as Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#>::defaultrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder][Default]default().() |
| 29 | } |
| 30 | |
| 31 | /// Set the max depth of the encoded object |
| 32 | #[must_use] |
| 33 | pub fn with_max_depthrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]with_max_depth().(mut self, max_depth: usize) -> Selfrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder] { |
| 34 | self.staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state..set_max_depthrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]set_max_depth().(max_depth); |
| 35 | self |
| 36 | } |
| 37 | |
| 38 | /// Emit a single token to the encoder |
| 39 | pub(craterust-analyzer cargo bendy 0.6.1 crate/) fn emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(&mut self, token: Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 40 | self.staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state..check_errorrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]check_error().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 41 | self.staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state..observe_tokenrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]observe_token().(&token)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 42 | match token { |
| 43 | Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Listrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#List# => self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'l'), |
| 44 | Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Dictrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#Dict# => self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'd'), |
| 45 | Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Stringrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#String#(s) => { |
| 46 | // Writing to a vec can't fail |
| 47 | let length = s.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 48 | self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(length.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().()); |
| 49 | self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b':'); |
| 50 | self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(s); |
| 51 | }, |
| 52 | Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Numrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#Num#(num) => { |
| 53 | // Alas, this doesn't verify that the given number is valid |
| 54 | self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'i'); |
| 55 | self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(num.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()); |
| 56 | self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'e'); |
| 57 | }, |
| 58 | Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Endrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#End# => self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'e'), |
| 59 | } |
| 60 | |
| 61 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 62 | } |
| 63 | |
| 64 | /// Emit an arbitrary encodable object |
| 65 | pub fn emitrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit().<E: ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#>(&mut self, value: E) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 66 | self.emit_withrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_with().(|e| value.encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#encode().(e)) |
| 67 | } |
| 68 | |
| 69 | /// Emit a single object using an encoder |
| 70 | pub fn emit_withrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_with().<F>(&mut self, value_cb: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 71 | where |
| 72 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 73 | { |
| 74 | let mut value_written = false; |
| 75 | let ret = value_cb(SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder# { |
| 76 | encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#encoder.: self, |
| 77 | value_writtenrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#value_written.: &mut value_written, |
| 78 | }); |
| 79 | |
| 80 | self.staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state..latch_errrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]latch_err().(ret)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 81 | |
| 82 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().value_written { |
| 83 | return self |
| 84 | .staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state. |
| 85 | .latch_errrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]latch_err().(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#::fromrust-analyzer cargo bendy 0.6.1 encoding/error/impl#[Error][`From<state_tracker::StructureError>`]from().(StructureErrorrust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#::invalid_staterust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/impl#[StructureError]invalid_state().( |
| 86 | "No value was emitted", |
| 87 | )))); |
| 88 | } |
| 89 | |
| 90 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 91 | } |
| 92 | |
| 93 | /// Emit an integer |
| 94 | pub fn emit_intrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_int().<T: PrintableIntegerrust-analyzer cargo bendy 0.6.1 encoding/printable_integer/PrintableInteger#>(&mut self, value: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 95 | // This doesn't use emit_token, as that would require that I write the integer to a |
| 96 | // temporary buffer and then copy it to the output; writing it directly saves at |
| 97 | // least one memory allocation |
| 98 | self.staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state..check_errorrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]check_error().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 99 | // We observe an int here, as we need something that isn't a string (and therefore |
| 100 | // possibly valid as a key) but we also want to require as few state transitions as |
| 101 | // possible (for performance) |
| 102 | self.staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state..observe_tokenrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]observe_token().(&Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Numrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#Num#(""))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 103 | self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'i'); |
| 104 | self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(value.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().()); |
| 105 | self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'e'); |
| 106 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 107 | } |
| 108 | |
| 109 | /// Emit a string |
| 110 | pub fn emit_strrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_str().(&mut self, value: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 111 | self.emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Stringrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#String#(value.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())) |
| 112 | } |
| 113 | |
| 114 | /// Emit a byte array |
| 115 | pub fn emit_bytesrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_bytes().(&mut self, value: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 116 | self.emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Stringrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#String#(value)) |
| 117 | } |
| 118 | |
| 119 | /// Emit a dictionary where you know that the keys are already |
| 120 | /// sorted. The callback must emit key/value pairs to the given |
| 121 | /// encoder in sorted order. If the key/value pairs may not be |
| 122 | /// sorted, [`emit_unsorted_dict`] should be used instead. |
| 123 | /// |
| 124 | /// [`emit_unsorted_dict`]: SingleItemEncoder::emit_unsorted_dict |
| 125 | /// |
| 126 | /// Example: |
| 127 | /// |
| 128 | /// ``` |
| 129 | /// # use bendy::encoding::{Encoder, Error}; |
| 130 | /// # |
| 131 | /// # fn main() -> Result<(), Error>{ |
| 132 | /// let mut encoder = Encoder::new(); |
| 133 | /// encoder.emit_dict(|mut e| { |
| 134 | /// e.emit_pair(b"a", "foo")?; |
| 135 | /// e.emit_pair(b"b", 2) |
| 136 | /// }) |
| 137 | /// # } |
| 138 | /// ``` |
| 139 | pub fn emit_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_dict().<F>(&mut self, content_cb: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 140 | where |
| 141 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(SortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 142 | { |
| 143 | self.emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Dictrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#Dict#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 144 | content_cb(SortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder# { encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder#encoder.: self })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 145 | self.emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Endrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#End#) |
| 146 | } |
| 147 | |
| 148 | /// Emit an arbitrary list. The callback should emit the contents |
| 149 | /// of the list to the given encoder. |
| 150 | /// |
| 151 | /// E.g., to emit the list `[1,2,3]`, you would write |
| 152 | /// |
| 153 | /// ``` |
| 154 | /// # use bendy::encoding::{Encoder, Error}; |
| 155 | /// # fn main() -> Result<(), Error> { |
| 156 | /// let mut encoder = Encoder::new(); |
| 157 | /// encoder.emit_list(|e| { |
| 158 | /// e.emit_int(1)?; |
| 159 | /// e.emit_int(2)?; |
| 160 | /// e.emit_int(3) |
| 161 | /// }) |
| 162 | /// # } |
| 163 | /// ``` |
| 164 | pub fn emit_listrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_list().<F>(&mut self, list_cb: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 165 | where |
| 166 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&mut Encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 167 | { |
| 168 | self.emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Listrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#List#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 169 | list_cb(self)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 170 | self.emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Endrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#End#) |
| 171 | } |
| 172 | |
| 173 | /// Emit a dictionary that may have keys out of order. This will write the dict |
| 174 | /// values to temporary memory, then sort them before adding them to the serialized |
| 175 | /// stream |
| 176 | /// |
| 177 | /// Example. |
| 178 | /// |
| 179 | /// ``` |
| 180 | /// # use bendy::encoding::{Encoder, Error}; |
| 181 | /// # |
| 182 | /// # fn main() -> Result<(), Error> { |
| 183 | /// let mut encoder = Encoder::new(); |
| 184 | /// encoder.emit_and_sort_dict(|e| { |
| 185 | /// // Unlike in the example for Encoder::emit_dict(), these keys aren't sorted |
| 186 | /// e.emit_pair(b"b", 2)?; |
| 187 | /// e.emit_pair(b"a", "foo") |
| 188 | /// }) |
| 189 | /// # } |
| 190 | /// ``` |
| 191 | pub fn emit_and_sort_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_and_sort_dict().<F>(&mut self, content_cb: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 192 | where |
| 193 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&mut UnsortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 194 | { |
| 195 | let mut encoder = self.begin_unsorted_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]begin_unsorted_dict().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 196 | |
| 197 | content_cb(&mut encoder)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 198 | |
| 199 | self.end_unsorted_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]end_unsorted_dict().(encoder) |
| 200 | } |
| 201 | |
| 202 | /// Return the encoded string, if all objects written are complete |
| 203 | pub fn get_outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]get_output().(mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 204 | self.staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state..observe_eofrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]observe_eof().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 205 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output.) |
| 206 | } |
| 207 | |
| 208 | pub(craterust-analyzer cargo bendy 0.6.1 crate/) fn begin_unsorted_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]begin_unsorted_dict().(&mut self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<UnsortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#, Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 209 | // emit the dict token so that a pre-existing state error is reported early |
| 210 | self.emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Dictrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#Dict#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 211 | |
| 212 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(UnsortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#::newrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder]new().(self.staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state..remaining_depthrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]remaining_depth().())) |
| 213 | } |
| 214 | |
| 215 | pub(craterust-analyzer cargo bendy 0.6.1 crate/) fn end_unsorted_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]end_unsorted_dict().(&mut self, encoder: UnsortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 216 | let content = encoder.donerust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder]done().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 217 | |
| 218 | for (k, v) in content { |
| 219 | self.emit_bytesrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_bytes().(&k)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 220 | // We know that the output is a single object by construction |
| 221 | self.staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state..observe_tokenrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]observe_token().(&Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Numrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#Num#(""))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 222 | self.outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#output..extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&v); |
| 223 | } |
| 224 | |
| 225 | self.emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Endrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#End#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 226 | |
| 227 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /// An encoder that can only encode a single item. See [`Encoder`] |
| 232 | /// for usage examples; the only difference between these classes is |
| 233 | /// that `SingleItemEncoder` can only be used once. |
| 234 | pub struct SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#<'a> { |
| 235 | encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#encoder.: &'a mut Encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#, |
| 236 | /// Whether we attempted to write a value to the encoder. The value |
| 237 | /// of the referent of this field is meaningless if the encode method |
| 238 | /// failed. |
| 239 | value_writtenrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#value_written.: &'a mut bool, |
| 240 | } |
| 241 | |
| 242 | impl<'a> SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#<'a> { |
| 243 | /// Emit an arbitrary encodable object |
| 244 | pub fn emitrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit().<E: ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(self, value: &E) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 245 | value.encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#encode().(self) |
| 246 | } |
| 247 | |
| 248 | /// Emit a single object using an encoder |
| 249 | pub fn emit_withrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_with().<F>(self, value_cb: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 250 | where |
| 251 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 252 | { |
| 253 | value_cb(self) |
| 254 | } |
| 255 | |
| 256 | /// Emit an integer |
| 257 | pub fn emit_intrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_int().<T: PrintableIntegerrust-analyzer cargo bendy 0.6.1 encoding/printable_integer/PrintableInteger#>(self, value: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 258 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self.value_writtenrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#value_written. = true; |
| 259 | self.encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#encoder..emit_intrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_int().(value) |
| 260 | } |
| 261 | |
| 262 | /// Emit a string |
| 263 | pub fn emit_strrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_str().(self, value: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 264 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self.value_writtenrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#value_written. = true; |
| 265 | self.encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#encoder..emit_strrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_str().(value) |
| 266 | } |
| 267 | |
| 268 | /// Emit a byte array |
| 269 | pub fn emit_bytesrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_bytes().(self, value: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 270 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self.value_writtenrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#value_written. = true; |
| 271 | self.encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#encoder..emit_bytesrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_bytes().(value) |
| 272 | } |
| 273 | |
| 274 | /// Emit an arbitrary list |
| 275 | pub fn emit_listrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_list().<F>(self, list_cb: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 276 | where |
| 277 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&mut Encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 278 | { |
| 279 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self.value_writtenrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#value_written. = true; |
| 280 | self.encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#encoder..emit_listrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_list().(list_cb) |
| 281 | } |
| 282 | |
| 283 | /// Emit a sorted dictionary. If the input dictionary is unsorted, this will return an error. |
| 284 | pub fn emit_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_dict().<F>(self, content_cb: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 285 | where |
| 286 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(SortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 287 | { |
| 288 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self.value_writtenrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#value_written. = true; |
| 289 | self.encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#encoder..emit_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_dict().(content_cb) |
| 290 | } |
| 291 | |
| 292 | /// Emit a dictionary that may have keys out of order. This will write the dict |
| 293 | /// values to temporary memory, then sort them before adding them to the serialized |
| 294 | /// stream |
| 295 | pub fn emit_unsorted_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_unsorted_dict().<F>(self, content_cb: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 296 | where |
| 297 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&mut UnsortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 298 | { |
| 299 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().self.value_writtenrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#value_written. = true; |
| 300 | self.encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#encoder..emit_and_sort_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_and_sort_dict().(content_cb) |
| 301 | } |
| 302 | |
| 303 | /// Emit an arbitrary list. |
| 304 | /// |
| 305 | /// Attention: If this method is used while canonical output is required |
| 306 | /// the caller needs to ensure that the iterator has a defined order. |
| 307 | pub fn emit_unchecked_listrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_unchecked_list().( |
| 308 | self, |
| 309 | iterable: impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = impl ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#>, |
| 310 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 311 | self.emit_listrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_list().(|e| { |
| 312 | for item in iterable { |
| 313 | e.emitrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit().(item)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 314 | } |
| 315 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 316 | }) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /// Encodes a map with pre-sorted keys |
| 321 | pub struct SortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder#<'a> { |
| 322 | encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder#encoder.: &'a mut Encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#, |
| 323 | } |
| 324 | |
| 325 | impl<'a> SortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder#<'a> { |
| 326 | /// Emit a key/value pair |
| 327 | pub fn emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().<E>(&mut self, key: &[u8], value: E) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 328 | where |
| 329 | E: ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#, |
| 330 | { |
| 331 | self.encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder#encoder..emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Stringrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#String#(key))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 332 | self.encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder#encoder..emitrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit().(value) |
| 333 | } |
| 334 | |
| 335 | /// Equivalent to [`SortedDictEncoder::emit_pair()`], but forces the type of the value |
| 336 | /// to be a callback |
| 337 | pub fn emit_pair_withrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair_with().<F>(&mut self, key: &[u8], value_cb: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 338 | where |
| 339 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 340 | { |
| 341 | self.encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder#encoder..emit_tokenrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_token().(Tokenrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#::Stringrust-analyzer cargo bendy 0.6.1 state_tracker/token/Token#String#(key))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 342 | self.encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SortedDictEncoder#encoder..emit_withrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_with().(value_cb) |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /// Helper to write a dictionary that may have keys out of order. This will buffer the |
| 347 | /// dict values in temporary memory, then sort them before adding them to the serialized |
| 348 | /// stream |
| 349 | pub struct UnsortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder# { |
| 350 | contentrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#content.: BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>, |
| 351 | errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error.: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 352 | remaining_depthrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#remaining_depth.: usize, |
| 353 | } |
| 354 | |
| 355 | impl UnsortedDictEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder# { |
| 356 | pub(craterust-analyzer cargo bendy 0.6.1 crate/) fn newrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder]new().(remaining_depth: usize) -> Selfrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder] { |
| 357 | Selfrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder] { |
| 358 | contentrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#content.: BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V>`]new().(), |
| 359 | errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error.: Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()), |
| 360 | remaining_depthrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#remaining_depth., |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /// Emit a key/value pair |
| 365 | pub fn emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder]emit_pair().<E>(&mut self, key: &[u8], value: E) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 366 | where |
| 367 | E: ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#, |
| 368 | { |
| 369 | self.emit_pair_withrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder]emit_pair_with().(key, |e| value.encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#encode().(e)) |
| 370 | } |
| 371 | |
| 372 | /// Emit a key/value pair where the value is produced by a callback |
| 373 | pub fn emit_pair_withrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder]emit_pair_with().<F>(&mut self, key: &[u8], value_cb: F) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> |
| 374 | where |
| 375 | F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#>, |
| 376 | { |
| 377 | let mut value_written = false; |
| 378 | |
| 379 | let mut encoder = Encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#::newrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]new().().with_max_depthrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]with_max_depth().(self.remaining_depthrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#remaining_depth.); |
| 380 | |
| 381 | let ret = value_cb(SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder# { |
| 382 | encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#encoder.: &mut encoder, |
| 383 | value_writtenrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#value_written.: &mut value_written, |
| 384 | }); |
| 385 | |
| 386 | if ret.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() { |
| 387 | self.errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error. = ret.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Clone]clone().(); |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().value_written { |
| 392 | self.errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error. = Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#::fromrust-analyzer cargo bendy 0.6.1 encoding/error/impl#[Error][`From<state_tracker::StructureError>`]from().(StructureErrorrust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#::InvalidStaterust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#InvalidState# { |
| 393 | staterust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#InvalidState#state.: "No value was emitted".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), |
| 394 | })); |
| 395 | } else { |
| 396 | self.errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error. = encoder.staterust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#state..observe_eofrust-analyzer cargo bendy 0.6.1 state_tracker/state/impl#[`StateTracker<S, E>`]observe_eof().(); |
| 397 | } |
| 398 | |
| 399 | if self.errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error..is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() { |
| 400 | return self.errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Clone]clone().(); |
| 401 | } |
| 402 | |
| 403 | let encoded_object = encoder |
| 404 | .get_outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]get_output().() |
| 405 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Any errors should have been caught by observe_eof"); |
| 406 | |
| 407 | self.save_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder]save_pair().(key, encoded_object) |
| 408 | } |
| 409 | |
| 410 | #[cfg(feature = "serde")] |
| 411 | pub(craterust-analyzer cargo bendy 0.6.1 crate/) fn remaining_depthrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder]remaining_depth().(&self) -> usize { |
| 412 | self.remaining_depthrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#remaining_depth. |
| 413 | } |
| 414 | |
| 415 | pub(craterust-analyzer cargo bendy 0.6.1 crate/) fn save_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder]save_pair().( |
| 416 | &mut self, |
| 417 | unencoded_key: &[u8], |
| 418 | encoded_value: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 419 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 420 | #[cfg(not(feature = "std"))] |
| 421 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::collectionsrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/::btree_maprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree_map/::Entryrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/entry/Entry#; |
| 422 | #[cfg(feature = "std")] |
| 423 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::btree_maprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree_map/::Entryrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/entry/Entry#; |
| 424 | |
| 425 | if self.errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error..is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().() { |
| 426 | return self.errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Clone]clone().(); |
| 427 | } |
| 428 | |
| 429 | let vacancy = match self.contentrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#content..entryrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]entry().(unencoded_key.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`][ToOwned]to_owned().()) { |
| 430 | Entryrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/entry/Entry#::Vacantrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/entry/Entry#Vacant#(vacancy) => vacancy, |
| 431 | Entryrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/entry/Entry#::Occupiedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/entry/Entry#Occupied#(occupation) => { |
| 432 | self.errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error. = Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#::fromrust-analyzer cargo bendy 0.6.1 encoding/error/impl#[Error][`From<state_tracker::StructureError>`]from().(StructureErrorrust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#::InvalidStaterust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#InvalidState# { |
| 433 | staterust-analyzer cargo bendy 0.6.1 state_tracker/structure_error/StructureError#InvalidState#state.: formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 434 | "Duplicate key {}", |
| 435 | Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8_lossyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8_lossy().(occupation.keyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/entry/impl#[`OccupiedEntry<'a, K, V, A>`]key().()) |
| 436 | ), |
| 437 | })); |
| 438 | return self.errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Clone]clone().(); |
| 439 | }, |
| 440 | }; |
| 441 | |
| 442 | vacancy.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/entry/impl#[`VacantEntry<'a, K, V, A>`]insert().(encoded_value); |
| 443 | |
| 444 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 445 | } |
| 446 | |
| 447 | pub(craterust-analyzer cargo bendy 0.6.1 crate/) fn donerust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[UnsortedDictEncoder]done().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>, Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#> { |
| 448 | self.errorrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#error.?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 449 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.contentrust-analyzer cargo bendy 0.6.1 encoding/encoder/UnsortedDictEncoder#content.) |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | #[cfg(test)] |
| 454 | mod testrust-analyzer cargo bendy 0.6.1 encoding/encoder/test/ { |
| 455 | use superrust-analyzer cargo bendy 0.6.1 encoding/encoder/::*; |
| 456 | |
| 457 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 458 | pub fn simple_encoding_worksrust-analyzer cargo bendy 0.6.1 encoding/encoder/test/simple_encoding_works().() { |
| 459 | let mut encoder = Encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#::newrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]new().(); |
| 460 | encoder |
| 461 | .emit_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_dict().(|mut e| { |
| 462 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"bar", 25)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 463 | e.emit_pair_withrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair_with().(b"foo", |e| { |
| 464 | e.emit_listrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_list().(|e| { |
| 465 | e.emit_strrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_str().("baz")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 466 | e.emit_strrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_str().("qux") |
| 467 | }) |
| 468 | }) |
| 469 | }) |
| 470 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Encoding shouldn't fail"); |
| 471 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 472 | &encoder |
| 473 | .get_outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]get_output().() |
| 474 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Complete object should have been written"), |
| 475 | &b"d3:bari25e3:fool3:baz3:quxee" |
| 476 | ); |
| 477 | } |
| 478 | |
| 479 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 480 | fn emit_cb_must_emitrust-analyzer cargo bendy 0.6.1 encoding/encoder/test/emit_cb_must_emit().() { |
| 481 | let mut encoder = Encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#::newrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]new().(); |
| 482 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(encoder.emit_withrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_with().(|_| Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 483 | } |
| 484 | } |