| 1 | #[cfg(not(feature = "std"))] |
| 2 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::{ |
| 3 | 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#, LinkedListrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/linked_list/LinkedList#, VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#}, |
| 4 | rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/::Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#, |
| 5 | 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#, |
| 6 | syncrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#, |
| 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 | |
| 10 | #[cfg(feature = "std")] |
| 11 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 12 | 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#, HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#, LinkedListrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/linked_list/LinkedList#, VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque#}, |
| 13 | hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::{BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher#, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#}, |
| 14 | rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/::Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#, |
| 15 | syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#, |
| 16 | }; |
| 17 | |
| 18 | use craterust-analyzer cargo bendy 0.6.1 crate/::encodingrust-analyzer cargo bendy 0.6.1 encoding/::{Encoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/Encoder#, Errorrust-analyzer cargo bendy 0.6.1 encoding/error/Error#, SingleItemEncoderrust-analyzer cargo bendy 0.6.1 encoding/encoder/SingleItemEncoder#}; |
| 19 | |
| 20 | /// An object that can be encoded into a single bencode object |
| 21 | pub trait ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# { |
| 22 | /// The maximum depth that this object could encode to. Leaves do not consume a level, so an |
| 23 | /// `i1e` has depth 0 and `li1ee` has depth 1. |
| 24 | /// |
| 25 | /// A depth of 0 may also indicate a statically unknown depth. See the |
| 26 | /// [`encoding`][crate::encoding#dynamic-depth] module docs. |
| 27 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#MAX_DEPTH.: usize; |
| 28 | |
| 29 | /// Encode this object into the bencode stream |
| 30 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#encode().(&self, encoder: 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#>; |
| 31 | |
| 32 | /// Encode this object to a byte string |
| 33 | fn to_bencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#to_bencode().(&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#> { |
| 34 | 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::MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#MAX_DEPTH.); |
| 35 | encoder.emit_withrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit_with().(|e| self.encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#encode().(e))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 36 | |
| 37 | let bytes = encoder.get_outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]get_output().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 38 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// Wrapper to allow `Vec<u8>` encoding as bencode string element. |
| 43 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)] |
| 44 | pub struct AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#<I>(pub I); |
| 45 | |
| 46 | // Forwarding impls |
| 47 | impl<'a, E: 'a + 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#> ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for &'a E { |
| 48 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`&'a E`][ToBencode]MAX_DEPTH.: usize = E::MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#MAX_DEPTH.; |
| 49 | |
| 50 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`&'a E`][ToBencode]encode().(&self, encoder: 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#> { |
| 51 | E::encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#encode().(self, encoder) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[cfg(feature = "std")] |
| 56 | impl<E: ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#> ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<E> { |
| 57 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`Box<E>`][ToBencode]MAX_DEPTH.: usize = E::MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#MAX_DEPTH.; |
| 58 | |
| 59 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`Box<E>`][ToBencode]encode().(&self, encoder: 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#> { |
| 60 | E::encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#encode().(self, encoder) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl<E: ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#> ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#<E> { |
| 65 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`Rc<E>`][ToBencode]MAX_DEPTH.: usize = E::MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#MAX_DEPTH.; |
| 66 | |
| 67 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`Rc<E>`][ToBencode]encode().(&self, encoder: 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#> { |
| 68 | E::encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#encode().(self, encoder) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl<E: ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#> ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<E> { |
| 73 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`Arc<E>`][ToBencode]MAX_DEPTH.: usize = E::MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#MAX_DEPTH.; |
| 74 | |
| 75 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`Arc<E>`][ToBencode]encode().(&self, encoder: 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#> { |
| 76 | E::encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#encode().(self, encoder) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Base type impls |
| 81 | impl ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for &str { |
| 82 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`&str`][ToBencode]MAX_DEPTH.: usize = 0; |
| 83 | |
| 84 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`&str`][ToBencode]encode().(&self, encoder: 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#> { |
| 85 | encoder.emit_strrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_str().(self) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 90 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[String][ToBencode]MAX_DEPTH.: usize = 0; |
| 91 | |
| 92 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[String][ToBencode]encode().(&self, encoder: 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#> { |
| 93 | encoder.emit_strrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_str().(self) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | macro_rules! impl_encodable_integerrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl_encodable_integer! { |
| 98 | ($($type:ty)*) => {$( |
| 99 | impl ToBencode for $type { |
| 100 | const MAX_DEPTH: usize = 1; |
| 101 | |
| 102 | fn encode(&self, encoder: SingleItemEncoder) -> Result<(), Error> { |
| 103 | encoder.emit_int(*self).map_err(Error::from) |
| 104 | } |
| 105 | } |
| 106 | )*} |
| 107 | } |
| 108 | |
| 109 | impl_encodable_integerrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl_encodable_integer!!(u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize); |
| 110 | |
| 111 | macro_rules! impl_encodable_iterablerust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl_encodable_iterable! { |
| 112 | ($($type:ident)*) => {$( |
| 113 | impl <ContentT> ToBencode for $type<ContentT> |
| 114 | where |
| 115 | ContentT: ToBencode |
| 116 | { |
| 117 | const MAX_DEPTH: usize = ContentT::MAX_DEPTH + 1; |
| 118 | |
| 119 | fn encode(&self, encoder: SingleItemEncoder) -> Result<(), Error> { |
| 120 | encoder.emit_list(|e| { |
| 121 | for item in self { |
| 122 | e.emit(item)?; |
| 123 | } |
| 124 | Ok(()) |
| 125 | })?; |
| 126 | |
| 127 | Ok(()) |
| 128 | } |
| 129 | } |
| 130 | )*} |
| 131 | } |
| 132 | |
| 133 | impl_encodable_iterablerust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl_encodable_iterable!!(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec# VecDequerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/vec_deque/VecDeque# LinkedListrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/linked_list/LinkedList#); |
| 134 | |
| 135 | impl<ContentT> ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for &[ContentT] |
| 136 | where |
| 137 | ContentT: ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#, |
| 138 | { |
| 139 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`&[ContentT]`][ToBencode]MAX_DEPTH.: usize = ContentT::MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#MAX_DEPTH. 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().1; |
| 140 | |
| 141 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`&[ContentT]`][ToBencode]encode().(&self, encoder: 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#> { |
| 142 | encoder.emit_listrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_list().(|e| { |
| 143 | for item in *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self { |
| 144 | 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().; |
| 145 | } |
| 146 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 147 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 148 | |
| 149 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | impl<K: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>, V: ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#> ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<K, V> { |
| 154 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`BTreeMap<K, V>`][ToBencode]MAX_DEPTH.: usize = V::MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#MAX_DEPTH. 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().1; |
| 155 | |
| 156 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`BTreeMap<K, V>`][ToBencode]encode().(&self, encoder: 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#> { |
| 157 | encoder.emit_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_dict().(|mut e| { |
| 158 | for (k, v) in self { |
| 159 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(k.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), v)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 160 | } |
| 161 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 162 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 163 | |
| 164 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | #[cfg(feature = "std")] |
| 169 | impl<K, V, S> ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, V, S> |
| 170 | where |
| 171 | K: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]> + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# + Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#, |
| 172 | V: ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#, |
| 173 | S: BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher#, |
| 174 | { |
| 175 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`HashMap<K, V, S>`][ToBencode]MAX_DEPTH.: usize = V::MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode#MAX_DEPTH. 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().1; |
| 176 | |
| 177 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`HashMap<K, V, S>`][ToBencode]encode().(&self, encoder: 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#> { |
| 178 | encoder.emit_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_dict().(|mut e| { |
| 179 | let mut pairs = self |
| 180 | .iterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]iter().() |
| 181 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(k, v)| (k.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().(), v)) |
| 182 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(); |
| 183 | pairs.sort_by_keyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by_key().(|&(k, _)| k); |
| 184 | for (k, v) in pairs { |
| 185 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(k, v)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 186 | } |
| 187 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 188 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 189 | |
| 190 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | impl<I> ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#<I> |
| 195 | where |
| 196 | I: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>, |
| 197 | { |
| 198 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`AsString<I>`][ToBencode]MAX_DEPTH.: usize = 1; |
| 199 | |
| 200 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`AsString<I>`][ToBencode]encode().(&self, encoder: 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#> { |
| 201 | encoder.emit_bytesrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_bytes().(self.0rust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#0..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 202 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | impl<I> AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]> for AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#<I> |
| 207 | where |
| 208 | I: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>, |
| 209 | { |
| 210 | fn as_refrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`AsString<I>`][`AsRef<[u8]>`]as_ref().(&self) -> &'_ [u8] { |
| 211 | self.0rust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#0..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().() |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | impl<'a, I> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a [u8]> for AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#<I> |
| 216 | where |
| 217 | I: Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a [u8]>, |
| 218 | { |
| 219 | fn fromrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`AsString<I>`][`From<&'a [u8]>`]from().(content: &'a [u8]) -> Selfrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/impl#[`AsString<I>`][`From<&'a [u8]>`] { |
| 220 | AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#(I::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#from().(content)) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | #[cfg(test)] |
| 225 | mod testrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/ { |
| 226 | |
| 227 | #[cfg(not(feature = "std"))] |
| 228 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::{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#, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/}; |
| 229 | |
| 230 | use superrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/::*; |
| 231 | |
| 232 | struct Foorust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo# { |
| 233 | barrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo#bar.: u32, |
| 234 | bazrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo#baz.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 235 | quxrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo#qux.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 236 | } |
| 237 | |
| 238 | impl ToBencoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/ToBencode# for Foorust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo# { |
| 239 | const MAX_DEPTHrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/impl#[Foo][ToBencode]MAX_DEPTH.: usize = 2; |
| 240 | |
| 241 | fn encoderust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/impl#[Foo][ToBencode]encode().(&self, encoder: 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#> { |
| 242 | encoder.emit_dictrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SingleItemEncoder<'a>`]emit_dict().(|mut e| { |
| 243 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"bar", &self.barrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo#bar.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 244 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"baz", &self.bazrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo#baz.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 245 | e.emit_pairrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[`SortedDictEncoder<'a>`]emit_pair().(b"qux", AsStringrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/AsString#(&self.quxrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo#qux.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 246 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 247 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 248 | |
| 249 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 254 | fn simple_encodable_worksrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/simple_encodable_works().() { |
| 255 | 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().(); |
| 256 | encoder |
| 257 | .emitrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]emit().(Foorust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo# { |
| 258 | barrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo#bar.: 5, |
| 259 | bazrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo#baz.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["foo".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), "bar".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()], |
| 260 | quxrust-analyzer cargo bendy 0.6.1 encoding/to_bencode/test/Foo#qux.: b"qux".to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(), |
| 261 | }) |
| 262 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 263 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 264 | &encoder.get_outputrust-analyzer cargo bendy 0.6.1 encoding/encoder/impl#[Encoder]get_output().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., |
| 265 | &b"d3:bari5e3:bazl3:foo3:bare3:qux3:quxe"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). |
| 266 | ); |
| 267 | } |
| 268 | } |