| 1 | //! Reflection for testing |
| 2 | //! |
| 3 | //! Provides facilities for creating an AST-like object |
| 4 | //! tree from any valid bencode: [`Inspectable`]. Decoding bencode into |
| 5 | //! an [`Inspectable`] is done using recursion, so stack size limits |
| 6 | //! apply. |
| 7 | //! |
| 8 | //! Use for reflection, modifying, testing, and pretty printing. |
| 9 | //! |
| 10 | //! Not recommended for use in production code. |
| 11 | //! |
| 12 | //! # Overview |
| 13 | //! |
| 14 | //! See the Reflection chapter of the [README.md][r]. |
| 15 | //! |
| 16 | //! # Creating |
| 17 | //! |
| 18 | //! The [`Inspectable`] docs explain how to create an `Inspectable` to get started. |
| 19 | //! |
| 20 | //! # Displaying / Outputting |
| 21 | //! |
| 22 | //! * Use [`.emit`][Inspectable::emit] to get the proper serialized bencode representation as a |
| 23 | //! byte vector. |
| 24 | //! * Derives [`Debug`][core::fmt::Debug]. |
| 25 | //! * Provides [`.as_rust_string_literal`][Inspectable::as_rust_string_literal] for pretty printing. |
| 26 | //! Useful when inspecting structures, hard coding test cases, etc. |
| 27 | //! * Provided [`Display`][core::fmt::Display] implementation is similar to `.as_rust_string_literal`, |
| 28 | //! but does not insert newlines, tabs, etc. Useful for debug logs. |
| 29 | //! |
| 30 | //! # Coercion |
| 31 | //! |
| 32 | //! Mutable coercion of [`Inspectable`] to variant items: [`.string`][Inspectable::string], |
| 33 | //! [`.int`][`Inspectable::int`], [`.dict`][`Inspectable::dict`], [`.list`][`Inspectable::list`], |
| 34 | //! and [`.raw`][`Inspectable::raw`]. |
| 35 | //! |
| 36 | //! Immutable coercion: [`.string_ref`][`Inspectable::string_ref`], [`.int_ref`][`Inspectable::int_ref`], |
| 37 | //! [`.dict_ref`][`Inspectable::dict_ref`], [`.list_ref`][`Inspectable::list_ref`], |
| 38 | //! and [`.raw_ref`][`Inspectable::raw_ref`]. |
| 39 | //! |
| 40 | //! These coercion method names are reversed compared to the standard `_mut` idiom in Rust, because you |
| 41 | //! almost always want to mutate these objects, not just look at them. These methods will panic if |
| 42 | //! called on an [`Inspectable`] with the wrong enum variant. |
| 43 | //! |
| 44 | //! # Traversing |
| 45 | //! |
| 46 | //! Use [`inspect_path()`][ip] to create a [`PathBuilder`][pb], for use with |
| 47 | //! the [`.find`][Inspectable::find], [`.find_ref`][Inspectable::find_ref], |
| 48 | //! [`.clone_and_mutate`][Inspectable::clone_and_mutate], and [`.mutate`][Inspectable::mutate] |
| 49 | //! methods. These will drill down into the AST according to the |
| 50 | //! [`Step`][crate::inspect::traverse::Step]s in the [`Path`][pb]. |
| 51 | //! |
| 52 | //! Searches in path traversal are implemented using recursion, so stack limits may |
| 53 | //! apply. One can also use a combination of coercion and the following methods to |
| 54 | //! traverse the AST without using Paths: |
| 55 | //! |
| 56 | //! * [`InList`]: [`.nth`][InList::nth], [`.nth_ref`][InList::nth_ref] |
| 57 | //! * [`InDict`]: [`.nth`][InDict::nth], [`.nth_ref`][InDict::nth_ref], |
| 58 | //! and [`.entry`][InDict::entry], [`.entry_ref`][InDict::entry_ref]. |
| 59 | //! * [`InDictEntry`]: [`.key`][InDictEntry::key] and [`.value`][InDictEntry::value]. |
| 60 | //! |
| 61 | //! The traversal methods will all panic on failure. |
| 62 | //! |
| 63 | //! # Mutating |
| 64 | //! |
| 65 | //! There are many ways to mutate an [`Inspectable`] AST. Some of them require first coercing the |
| 66 | //! [`Inspectable`] to its appropriate subtype. These are all best applied using these functions: |
| 67 | //! [`.clone_and_mutate`][Inspectable::clone_and_mutate] and [`.mutate`][Inspectable::mutate]. |
| 68 | //! For convenience, there is also [`.apply`][Inspectable::apply]. |
| 69 | //! |
| 70 | //! Here are all the mutation methods, grouped by the type they belong to. Methods on [`Inspectable`] |
| 71 | //! may only be used on the type variants specified in parentheses. They will panic if used on a type |
| 72 | //! variant they do not support. All of these except `.replace` are also available on their respective |
| 73 | //! type variants. |
| 74 | //! |
| 75 | //! * [`Inspectable`] |
| 76 | //! * [`.clear_content`][Inspectable::clear_content] (all) |
| 77 | //! * [`.remove_entry`][`Inspectable::remove_entry`] (dicts only) |
| 78 | //! * [`.remove_nth`][`Inspectable::remove_nth`] (dicts and lists) |
| 79 | //! * [`.replace`][Inspectable::replace] (all) |
| 80 | //! * [`.set_content_byterange`][Inspectable::set_content_byterange] (bytestrings only) |
| 81 | //! * [`.truncate`][Inspectable::truncate] (bytestrings only) |
| 82 | //! * [`InDict`] |
| 83 | //! * [`.sort`][InDict::sort] |
| 84 | //! * [`InInt`] |
| 85 | //! * [`.set`][InInt::set] |
| 86 | //! * [`InString`] (bytestrings) |
| 87 | //! * [`.clear_fake_length`][InString::clear_fake_length] |
| 88 | //! * [`.set_fake_length`][InString::set_fake_length] |
| 89 | //! * [`.set_content_str`][InString::set_content_str] |
| 90 | //! * [`.set_content_string`][InString::set_content_string] |
| 91 | //! * [`.set_content_u8`][InString::set_content_u8] |
| 92 | //! * [`.set_content_vec`][InString::set_content_vec] |
| 93 | //! |
| 94 | //! All properties of [`Inspectable`] and its type variants are public, so you may also make your own mutators. |
| 95 | //! |
| 96 | //! [r]: https://github.com/P3KI/bendy/blob/master/README.md#reflection |
| 97 | //! [pb]: crate::inspect::traverse::PathBuilder |
| 98 | //! [ip]: crate::inspect::inspect_path |
| 99 | |
| 100 | #[cfg(not(feature = "std"))] |
| 101 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::{ |
| 102 | borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#, |
| 103 | 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#}, |
| 104 | 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#, |
| 105 | }; |
| 106 | #[cfg(feature = "std")] |
| 107 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#, 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#}; |
| 108 | |
| 109 | pub mod displayrust-analyzer cargo bendy 0.6.1 inspect/display/; |
| 110 | pub mod mutaterust-analyzer cargo bendy 0.6.1 inspect/mutate/; |
| 111 | #[doc(hidden)] |
| 112 | pub mod parserust-analyzer cargo bendy 0.6.1 inspect/parse/; |
| 113 | pub mod traverserust-analyzer cargo bendy 0.6.1 inspect/traverse/; |
| 114 | |
| 115 | /// Attempt to decode a u8 buffer into an inspectable. |
| 116 | /// Panics on error. Use [`Inspectable::try_from`] for a |
| 117 | /// fallible alternative. |
| 118 | pub fn inspectrust-analyzer cargo bendy 0.6.1 inspect/inspect().<'ser>(buf: &'ser [u8]) -> Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser> { |
| 119 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::try_fromrust-analyzer cargo bendy 0.6.1 inspect/parse/impl#[`Inspectable<'ser>`][`TryFrom<&'ser [u8]>`]try_from().(buf).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Could not decode buffer into inspectable") |
| 120 | } |
| 121 | |
| 122 | /// Builds a path that can be used to traverse from an Inspectable AST. |
| 123 | /// [Read here][crate::inspect#traversing] for details. |
| 124 | pub fn inspect_pathrust-analyzer cargo bendy 0.6.1 inspect/inspect_path().<'ser>() -> traverserust-analyzer cargo bendy 0.6.1 inspect/traverse/::PathBuilderrust-analyzer cargo bendy 0.6.1 inspect/traverse/PathBuilder#<'ser> { |
| 125 | traverserust-analyzer cargo bendy 0.6.1 inspect/traverse/::PathBuilderrust-analyzer cargo bendy 0.6.1 inspect/traverse/PathBuilder#::newrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`PathBuilder<'pb>`]new().() |
| 126 | } |
| 127 | |
| 128 | /// An object that represents something that bencode can |
| 129 | /// decode to. Usable for reflection, modification, |
| 130 | /// testing, and pretty printing. |
| 131 | /// |
| 132 | /// See the [module][crate::inspect] documentation for usage. |
| 133 | /// |
| 134 | /// # Construction |
| 135 | /// |
| 136 | /// You parse bencode into an Inspectable AST like this: |
| 137 | /// ``` |
| 138 | /// # use bendy; |
| 139 | /// # use bendy::inspect::*; |
| 140 | /// let bencode = b"i10e"; |
| 141 | /// let inspectable = bendy::inspect::inspect(bencode); |
| 142 | /// assert_eq!(inspectable.emit(), bencode); |
| 143 | /// ``` |
| 144 | /// |
| 145 | /// Or use the [`Inspectable::try_from`] method for fallible construction. |
| 146 | #[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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 147 | pub enum Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser> { |
| 148 | Intrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#Int#(InIntrust-analyzer cargo bendy 0.6.1 inspect/InInt#<'ser>), |
| 149 | Stringrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#String#(InStringrust-analyzer cargo bendy 0.6.1 inspect/InString#<'ser>), |
| 150 | Rawrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#Raw#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'ser, [u8]>), |
| 151 | Dictrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#Dict#(InDictrust-analyzer cargo bendy 0.6.1 inspect/InDict#<'ser>), |
| 152 | Listrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#List#(InListrust-analyzer cargo bendy 0.6.1 inspect/InList#<'ser>), |
| 153 | } |
| 154 | |
| 155 | impl<'ser> Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser> { |
| 156 | /// Construct a new [`Inspectable::Raw`] |
| 157 | pub fn new_rawrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]new_raw().(buf: &'ser [u8]) -> Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser> { |
| 158 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::Rawrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#Raw#(Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/cow/impl#[`Cow<'a, [T]>`][`From<&'a [T]>`]from().(buf)) |
| 159 | } |
| 160 | |
| 161 | /// Construct a new [`Inspectable::Int`] |
| 162 | pub fn new_intrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]new_int().(i: i64) -> Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser> { |
| 163 | let inint = InIntrust-analyzer cargo bendy 0.6.1 inspect/InInt# { |
| 164 | bytesrust-analyzer cargo bendy 0.6.1 inspect/InInt#bytes.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(i.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()), |
| 165 | }; |
| 166 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::Intrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#Int#(inint) |
| 167 | } |
| 168 | |
| 169 | /// Construct a new [`Inspectable::String`] |
| 170 | pub fn new_stringrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]new_string().(buf: &'ser [u8]) -> Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser> { |
| 171 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::Stringrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#String#(InStringrust-analyzer cargo bendy 0.6.1 inspect/InString#::newrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InString<'ser>`]new().(buf)) |
| 172 | } |
| 173 | |
| 174 | /// Construct a new [`Inspectable::Dict`] |
| 175 | pub fn new_dictrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]new_dict().() -> Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser> { |
| 176 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::Dictrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#Dict#(InDictrust-analyzer cargo bendy 0.6.1 inspect/InDict#::newrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InDict<'ser>`]new().()) |
| 177 | } |
| 178 | |
| 179 | /// Construct a new [`Inspectable::List`] |
| 180 | pub fn new_listrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]new_list().() -> Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser> { |
| 181 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::Listrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#List#(InListrust-analyzer cargo bendy 0.6.1 inspect/InList#::newrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InList<'ser>`]new().()) |
| 182 | } |
| 183 | |
| 184 | /// Get the serialized bencode representation of this [`Inspectable`] |
| 185 | pub fn emitrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]emit().(&self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> { |
| 186 | let mut res = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 187 | |
| 188 | fn emit_strrust-analyzer cargo bendy 0.6.1 inspect/emit_str().(s: &InStringrust-analyzer cargo bendy 0.6.1 inspect/InString#, out: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) { |
| 189 | let len = s.lenrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InString<'ser>`]len().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 190 | out.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(len.as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().()); |
| 191 | out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b':'); |
| 192 | out.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(s.contentrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InString<'ser>`]content().()) |
| 193 | } |
| 194 | |
| 195 | fn dispatchrust-analyzer cargo bendy 0.6.1 inspect/dispatch().(i: &Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#, out: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) { |
| 196 | match i { |
| 197 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::Stringrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#String#(x) => { |
| 198 | emit_strrust-analyzer cargo bendy 0.6.1 inspect/emit_str().(x, out); |
| 199 | }, |
| 200 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::Rawrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#Raw#(x) => { |
| 201 | out.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(x); |
| 202 | }, |
| 203 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::Intrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#Int#(x) => { |
| 204 | out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'i'); |
| 205 | out.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(x.bytesrust-analyzer cargo bendy 0.6.1 inspect/InInt#bytes..as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()); |
| 206 | out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'e'); |
| 207 | }, |
| 208 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::Listrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#List#(x) => { |
| 209 | out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'l'); |
| 210 | for item in x.itemsrust-analyzer cargo bendy 0.6.1 inspect/InList#items..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 211 | dispatchrust-analyzer cargo bendy 0.6.1 inspect/dispatch().(item, out); |
| 212 | } |
| 213 | out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'e'); |
| 214 | }, |
| 215 | Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#::Dictrust-analyzer cargo bendy 0.6.1 inspect/Inspectable#Dict#(x) => { |
| 216 | out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'd'); |
| 217 | for InDictEntryrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry# { keyrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#key., valuerust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#value. } in x.itemsrust-analyzer cargo bendy 0.6.1 inspect/InDict#items..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 218 | dispatchrust-analyzer cargo bendy 0.6.1 inspect/dispatch().(key, out); |
| 219 | dispatchrust-analyzer cargo bendy 0.6.1 inspect/dispatch().(value, out); |
| 220 | } |
| 221 | out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(b'e'); |
| 222 | }, |
| 223 | } |
| 224 | } |
| 225 | dispatchrust-analyzer cargo bendy 0.6.1 inspect/dispatch().(self, &mut res); |
| 226 | res |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /// An [`Inspectable`] AST node representing a byte string. |
| 231 | /// Stores the raw bytes of the bytestring, in a [`Cow`]. |
| 232 | #[doc(alias = "bytestring")] |
| 233 | #[doc(alias = "byte string")] |
| 234 | #[doc(alias = "string")] |
| 235 | #[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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 236 | pub struct InStringrust-analyzer cargo bendy 0.6.1 inspect/InString#<'ser> { |
| 237 | /// The bytes in this ByteString. Does not include |
| 238 | /// the prefix length and prefix separator `:`. |
| 239 | pub bytesrust-analyzer cargo bendy 0.6.1 inspect/InString#bytes.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'ser, [u8]>, |
| 240 | /// If set, will provide a fake length prefix when emitted. |
| 241 | pub fake_lengthrust-analyzer cargo bendy 0.6.1 inspect/InString#fake_length.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>, |
| 242 | } |
| 243 | |
| 244 | /// An [`Inspectable`] AST node representing an integer. |
| 245 | /// Stores the raw bytes of the integer, in a [`Cow`]. |
| 246 | #[doc(alias = "int")] |
| 247 | #[doc(alias = "number")] |
| 248 | #[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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 249 | pub struct InIntrust-analyzer cargo bendy 0.6.1 inspect/InInt#<'ser> { |
| 250 | /// The raw bytes making up the number. |
| 251 | /// Does not include the `i` prefix and `e` suffix. |
| 252 | pub bytesrust-analyzer cargo bendy 0.6.1 inspect/InInt#bytes.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'ser, str>, |
| 253 | } |
| 254 | |
| 255 | /// An [`Inspectable`] AST node representing a dictionary entry. |
| 256 | /// This only appears in the [`InDict`] node, never as a variant |
| 257 | /// of [`Inspectables`][Inspectable]. |
| 258 | #[doc(alias = "dict entry")] |
| 259 | #[doc(alias = "dictionary entry")] |
| 260 | #[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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 261 | pub struct InDictEntryrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#<'ser> { |
| 262 | /// This struct field MUST contain an Inspectable::String |
| 263 | /// for the resulting bencode to be valid. |
| 264 | pub keyrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#key.: Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser>, |
| 265 | pub valuerust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#value.: Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser>, |
| 266 | } |
| 267 | |
| 268 | /// An [`Inspectable`] AST node representing a dictionary. |
| 269 | #[doc(alias = "dict")] |
| 270 | #[doc(alias = "dictionary")] |
| 271 | #[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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 272 | pub struct InDictrust-analyzer cargo bendy 0.6.1 inspect/InDict#<'ser> { |
| 273 | /// The entries in the dictionary. |
| 274 | /// Note that bendy will NOT decode a dict |
| 275 | /// if the keys are unsorted. |
| 276 | pub itemsrust-analyzer cargo bendy 0.6.1 inspect/InDict#items.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<InDictEntryrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#<'ser>>, |
| 277 | } |
| 278 | |
| 279 | /// An [`Inspectable`] AST node representing a list. |
| 280 | #[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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 281 | #[doc(alias = "list")] |
| 282 | #[doc(alias = "vec")] |
| 283 | pub struct InListrust-analyzer cargo bendy 0.6.1 inspect/InList#<'ser> { |
| 284 | pub itemsrust-analyzer cargo bendy 0.6.1 inspect/InList#items.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser>>, |
| 285 | } |
| 286 | |
| 287 | impl<'ser> InIntrust-analyzer cargo bendy 0.6.1 inspect/InInt#<'ser> { |
| 288 | pub fn newrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InInt<'ser>`]new().(buf: &'ser str) -> Selfrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InInt<'ser>`] { |
| 289 | InIntrust-analyzer cargo bendy 0.6.1 inspect/InInt# { |
| 290 | bytesrust-analyzer cargo bendy 0.6.1 inspect/InInt#bytes.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'a, str>`][`From<&'a str>`]from().(buf), |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /// Returns an i64 of the value represented. |
| 295 | /// Panics if this is not possible. |
| 296 | #[must_use] |
| 297 | pub fn as_i64rust-analyzer cargo bendy 0.6.1 inspect/impl#[`InInt<'ser>`]as_i64().(&self) -> i64 { |
| 298 | self.bytesrust-analyzer cargo bendy 0.6.1 inspect/InInt#bytes..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Could not parse InInt as i64") |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | impl<'ser> InStringrust-analyzer cargo bendy 0.6.1 inspect/InString#<'ser> { |
| 303 | pub fn newrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InString<'ser>`]new().(buf: &'ser [u8]) -> InStringrust-analyzer cargo bendy 0.6.1 inspect/InString#<'ser> { |
| 304 | InStringrust-analyzer cargo bendy 0.6.1 inspect/InString# { |
| 305 | bytesrust-analyzer cargo bendy 0.6.1 inspect/InString#bytes.: Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/cow/impl#[`Cow<'a, [T]>`][`From<&'a [T]>`]from().(buf), |
| 306 | fake_lengthrust-analyzer cargo bendy 0.6.1 inspect/InString#fake_length.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /// Returns the number of bytes in the bytestring, |
| 311 | /// or the fake length of the bytestring if set. |
| 312 | pub fn lenrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InString<'ser>`]len().(&self) -> usize { |
| 313 | self.fake_lengthrust-analyzer cargo bendy 0.6.1 inspect/InString#fake_length..unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(|| self.bytesrust-analyzer cargo bendy 0.6.1 inspect/InString#bytes..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) |
| 314 | } |
| 315 | |
| 316 | /// Returns true if there are no bytes in the bytestring, |
| 317 | /// or if the fake length of the bytestring is 0 if set. |
| 318 | pub fn is_emptyrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InString<'ser>`]is_empty().(&self) -> bool { |
| 319 | self.lenrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InString<'ser>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 |
| 320 | } |
| 321 | |
| 322 | /// Convenience method to get an immutable reference |
| 323 | /// to the bytestring's contents. |
| 324 | pub fn contentrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InString<'ser>`]content().(&self) -> &[u8] { |
| 325 | &self.bytesrust-analyzer cargo bendy 0.6.1 inspect/InString#bytes. |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | impl<'ser> InListrust-analyzer cargo bendy 0.6.1 inspect/InList#<'ser> { |
| 330 | #[allow(clippy::new_without_default)] |
| 331 | pub fn newrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InList<'ser>`]new().() -> Selfrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InList<'ser>`] { |
| 332 | InListrust-analyzer cargo bendy 0.6.1 inspect/InList# { itemsrust-analyzer cargo bendy 0.6.1 inspect/InList#items.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().() } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | impl<'ser> InDictrust-analyzer cargo bendy 0.6.1 inspect/InDict#<'ser> { |
| 337 | #[allow(clippy::new_without_default)] |
| 338 | pub fn newrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InDict<'ser>`]new().() -> Selfrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InDict<'ser>`] { |
| 339 | InDictrust-analyzer cargo bendy 0.6.1 inspect/InDict# { itemsrust-analyzer cargo bendy 0.6.1 inspect/InDict#items.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().() } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | impl<'ser> InDictEntryrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#<'ser> { |
| 344 | /// Creates a new dict entry. If the given key is not a byte string |
| 345 | /// ([`Inspectable::String`]) then bendy will not decode the resulting bencode. |
| 346 | pub fn newrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InDictEntry<'ser>`]new().(key: Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser>, value: Inspectablerust-analyzer cargo bendy 0.6.1 inspect/Inspectable#<'ser>) -> Selfrust-analyzer cargo bendy 0.6.1 inspect/impl#[`InDictEntry<'ser>`] { |
| 347 | InDictEntryrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry# { keyrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#key., valuerust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#value. } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | #[cfg(test)] |
| 352 | mod testsrust-analyzer cargo bendy 0.6.1 inspect/tests/ { |
| 353 | use superrust-analyzer cargo bendy 0.6.1 inspect/::*; |
| 354 | |
| 355 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 356 | fn all_traversal_testrust-analyzer cargo bendy 0.6.1 inspect/tests/all_traversal_test().() { |
| 357 | let buf = b"\ |
| 358 | l\ |
| 359 | i99e\ |
| 360 | 5:hello\ |
| 361 | d\ |
| 362 | 3:one\ |
| 363 | i11e\ |
| 364 | 3:two\ |
| 365 | i22e\ |
| 366 | 5:zzzzz\ |
| 367 | i33e\ |
| 368 | e\ |
| 369 | e"; |
| 370 | let i = inspectrust-analyzer cargo bendy 0.6.1 inspect/inspect().(buf); |
| 371 | let l = i.list_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]list_ref().(); |
| 372 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(3, l.itemsrust-analyzer cargo bendy 0.6.1 inspect/InList#items..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 373 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(99, l.nth_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InList<'ser>`]nth_ref().(0).int_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]int_ref().().as_i64rust-analyzer cargo bendy 0.6.1 inspect/impl#[`InInt<'ser>`]as_i64().()); |
| 374 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"hello".as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(), &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().l.nth_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InList<'ser>`]nth_ref().(1).string_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string_ref().().bytesrust-analyzer cargo bendy 0.6.1 inspect/InString#bytes.); |
| 375 | let d = i.list_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]list_ref().().nth_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InList<'ser>`]nth_ref().(2).dict_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]dict_ref().(); |
| 376 | let t0 = d.nth_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InDict<'ser>`]nth_ref().(0); |
| 377 | let t1 = d.nth_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InDict<'ser>`]nth_ref().(1); |
| 378 | let t2 = d.entry_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InDict<'ser>`]entry_ref().(b"zzzzz"); |
| 379 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"one".as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(), &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().t0.keyrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#key..string_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string_ref().().bytesrust-analyzer cargo bendy 0.6.1 inspect/InString#bytes.); |
| 380 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"two".as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(), &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().t1.keyrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#key..string_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string_ref().().bytesrust-analyzer cargo bendy 0.6.1 inspect/InString#bytes.); |
| 381 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"zzzzz".as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().(), &*rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, B>`][Deref]deref().t2.keyrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#key..string_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string_ref().().bytesrust-analyzer cargo bendy 0.6.1 inspect/InString#bytes.); |
| 382 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(11, t0.valuerust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#value..int_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]int_ref().().as_i64rust-analyzer cargo bendy 0.6.1 inspect/impl#[`InInt<'ser>`]as_i64().()); |
| 383 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(22, t1.valuerust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#value..int_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]int_ref().().as_i64rust-analyzer cargo bendy 0.6.1 inspect/impl#[`InInt<'ser>`]as_i64().()); |
| 384 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(33, t2.valuerust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#value..int_refrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]int_ref().().as_i64rust-analyzer cargo bendy 0.6.1 inspect/impl#[`InInt<'ser>`]as_i64().()); |
| 385 | } |
| 386 | |
| 387 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 388 | fn int_modify_testrust-analyzer cargo bendy 0.6.1 inspect/tests/int_modify_test().() { |
| 389 | let buf = b"i64e"; |
| 390 | let mut i = inspectrust-analyzer cargo bendy 0.6.1 inspect/inspect().(buf); |
| 391 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(64, i.intrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]int().().as_i64rust-analyzer cargo bendy 0.6.1 inspect/impl#[`InInt<'ser>`]as_i64().()); |
| 392 | i.intrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]int().().setrust-analyzer cargo bendy 0.6.1 inspect/mutate/impl#[`InInt<'ser>`]set().(32); |
| 393 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(32, i.intrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]int().().as_i64rust-analyzer cargo bendy 0.6.1 inspect/impl#[`InInt<'ser>`]as_i64().()); |
| 394 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("i32e", i.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 395 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"i32e", i.emitrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]emit().().as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()); |
| 396 | |
| 397 | let buf = b"\ |
| 398 | l\ |
| 399 | i43770e\ |
| 400 | d\ |
| 401 | 3:one\ |
| 402 | i11e\ |
| 403 | e\ |
| 404 | e"; |
| 405 | let mut i = inspectrust-analyzer cargo bendy 0.6.1 inspect/inspect().(buf); |
| 406 | i.listrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]list().().nthrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InList<'ser>`]nth().(0).intrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]int().().setrust-analyzer cargo bendy 0.6.1 inspect/mutate/impl#[`InInt<'ser>`]set().(2); |
| 407 | i.listrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]list().().nthrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InList<'ser>`]nth().(1).dictrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]dict().().nthrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InDict<'ser>`]nth().(0).valuerust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#value..intrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]int().().setrust-analyzer cargo bendy 0.6.1 inspect/mutate/impl#[`InInt<'ser>`]set().(1); |
| 408 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"li2ed3:onei1eee", i.emitrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]emit().().as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()); |
| 409 | } |
| 410 | |
| 411 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 412 | fn fake_bytestring_length_testrust-analyzer cargo bendy 0.6.1 inspect/tests/fake_bytestring_length_test().() { |
| 413 | let buf = b"\ |
| 414 | l\ |
| 415 | 5:hello\ |
| 416 | d\ |
| 417 | 3:one\ |
| 418 | i11e\ |
| 419 | e\ |
| 420 | e"; |
| 421 | let mut i = inspectrust-analyzer cargo bendy 0.6.1 inspect/inspect().(buf); |
| 422 | i.listrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]list().().nthrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InList<'ser>`]nth().(0).stringrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string().().set_fake_lengthrust-analyzer cargo bendy 0.6.1 inspect/mutate/impl#[`InString<'ser>`]set_fake_length().(20); |
| 423 | i.listrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]list().() |
| 424 | .nthrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InList<'ser>`]nth().(1) |
| 425 | .dictrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]dict().() |
| 426 | .nthrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InDict<'ser>`]nth().(0) |
| 427 | .keyrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#key. |
| 428 | .stringrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string().() |
| 429 | .set_fake_lengthrust-analyzer cargo bendy 0.6.1 inspect/mutate/impl#[`InString<'ser>`]set_fake_length().(0); |
| 430 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"l20:hellod0:onei11eee", i.emitrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]emit().().as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()); |
| 431 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("l20:hellod0:onei11eee", i.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 432 | } |
| 433 | |
| 434 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 435 | fn bytestring_set_content_testrust-analyzer cargo bendy 0.6.1 inspect/tests/bytestring_set_content_test().() { |
| 436 | let buf = b"\ |
| 437 | l\ |
| 438 | 5:hello\ |
| 439 | 5:world\ |
| 440 | d\ |
| 441 | 5:hello\ |
| 442 | 5:world\ |
| 443 | e\ |
| 444 | e"; |
| 445 | let mut i = inspectrust-analyzer cargo bendy 0.6.1 inspect/inspect().(buf); |
| 446 | i.listrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]list().() |
| 447 | .nthrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InList<'ser>`]nth().(0) |
| 448 | .stringrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string().() |
| 449 | .set_content_stringrust-analyzer cargo bendy 0.6.1 inspect/mutate/impl#[`InString<'ser>`]set_content_string().("one".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 450 | i.listrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]list().().nthrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InList<'ser>`]nth().(1).stringrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string().().set_content_strrust-analyzer cargo bendy 0.6.1 inspect/mutate/impl#[`InString<'ser>`]set_content_str().("two"); |
| 451 | let entry = i.listrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]list().().nthrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InList<'ser>`]nth().(2).dictrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]dict().().nthrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`InDict<'ser>`]nth().(0); |
| 452 | entry.keyrust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#key..stringrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string().().set_content_u8rust-analyzer cargo bendy 0.6.1 inspect/mutate/impl#[`InString<'ser>`]set_content_u8().(b"three"); |
| 453 | entry.valuerust-analyzer cargo bendy 0.6.1 inspect/InDictEntry#value..stringrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string().().set_content_vecrust-analyzer cargo bendy 0.6.1 inspect/mutate/impl#[`InString<'ser>`]set_content_vec().(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`][`From<&[T; N]>`]from().(b"four")); |
| 454 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"l3:one3:twod5:three4:fouree", i.emitrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]emit().().as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()); |
| 455 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("l3:one3:twod5:three4:fouree", i.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 456 | |
| 457 | let mut i = inspectrust-analyzer cargo bendy 0.6.1 inspect/inspect().(b"5:hello"); |
| 458 | i.stringrust-analyzer cargo bendy 0.6.1 inspect/traverse/impl#[`Inspectable<'ser>`]string().().set_content_u8rust-analyzer cargo bendy 0.6.1 inspect/mutate/impl#[`InString<'ser>`]set_content_u8().(b"\x00\x01"); |
| 459 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(b"2:\x00\x01", i.emitrust-analyzer cargo bendy 0.6.1 inspect/impl#[`Inspectable<'ser>`]emit().().as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()); |
| 460 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("2:\\x00\\x01", i.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()); |
| 461 | } |
| 462 | } |