@@ -52,6 +52,7 @@ impl CompactBytecode {
52
52
pub fn empty ( ) -> Self {
53
53
Self { object : Default :: default ( ) , source_map : None , link_references : Default :: default ( ) }
54
54
}
55
+
55
56
/// Returns the parsed source map
56
57
///
57
58
/// See also <https://docs.soliditylang.org/en/v0.8.10/internals/source_mappings.html>
@@ -89,6 +90,16 @@ impl CompactBytecode {
89
90
}
90
91
false
91
92
}
93
+
94
+ /// Returns the bytes of the bytecode object.
95
+ pub fn bytes ( & self ) -> Option < & Bytes > {
96
+ self . object . as_bytes ( )
97
+ }
98
+
99
+ /// Returns the underlying `Bytes` if the object is a valid bytecode.
100
+ pub fn into_bytes ( self ) -> Option < Bytes > {
101
+ self . object . into_bytes ( )
102
+ }
92
103
}
93
104
94
105
impl From < Bytecode > for CompactBytecode {
@@ -203,78 +214,88 @@ impl Bytecode {
203
214
}
204
215
false
205
216
}
217
+
218
+ /// Returns a reference to the underlying `Bytes` if the object is a valid bytecode.
219
+ pub fn bytes ( & self ) -> Option < & Bytes > {
220
+ self . object . as_bytes ( )
221
+ }
222
+
223
+ /// Returns the underlying `Bytes` if the object is a valid bytecode.
224
+ pub fn into_bytes ( self ) -> Option < Bytes > {
225
+ self . object . into_bytes ( )
226
+ }
206
227
}
207
228
208
229
/// Represents the bytecode of a contracts that might be not fully linked yet.
209
230
#[ derive( Clone , Debug , Serialize , Deserialize , Eq , PartialEq ) ]
210
231
#[ serde( untagged) ]
211
232
pub enum BytecodeObject {
212
- /// Fully linked bytecode object
233
+ /// Fully linked bytecode object.
213
234
#[ serde( deserialize_with = "serde_helpers::deserialize_bytes" ) ]
214
235
Bytecode ( Bytes ) ,
215
- /// Bytecode as hex string that's not fully linked yet and contains library placeholders
236
+ /// Bytecode as hex string that's not fully linked yet and contains library placeholders.
216
237
#[ serde( with = "serde_helpers::string_bytes" ) ]
217
238
Unlinked ( String ) ,
218
239
}
219
240
220
241
impl BytecodeObject {
221
- /// Returns the underlying `Bytes` if the object is a valid bytecode, and not empty
222
- pub fn into_bytes ( self ) -> Option < Bytes > {
242
+ /// Returns a reference to the underlying `Bytes` if the object is a valid bytecode.
243
+ pub fn as_bytes ( & self ) -> Option < & Bytes > {
223
244
match self {
224
245
BytecodeObject :: Bytecode ( bytes) => Some ( bytes) ,
225
246
BytecodeObject :: Unlinked ( _) => None ,
226
247
}
227
248
}
228
249
229
- /// Returns a reference to the underlying `Bytes` if the object is a valid bytecode, and not
230
- /// empty
231
- pub fn as_bytes ( & self ) -> Option < & Bytes > {
250
+ /// Returns the underlying `Bytes` if the object is a valid bytecode.
251
+ pub fn into_bytes ( self ) -> Option < Bytes > {
232
252
match self {
233
253
BytecodeObject :: Bytecode ( bytes) => Some ( bytes) ,
234
254
BytecodeObject :: Unlinked ( _) => None ,
235
255
}
236
256
}
237
257
238
- /// Returns the number of bytes of the fully linked bytecode
258
+ /// Returns the number of bytes of the fully linked bytecode.
239
259
///
240
260
/// Returns `0` if this object is unlinked.
241
261
pub fn bytes_len ( & self ) -> usize {
242
262
self . as_bytes ( ) . map ( |b| b. as_ref ( ) . len ( ) ) . unwrap_or_default ( )
243
263
}
244
264
245
- /// Returns a reference to the underlying `String` if the object is unlinked
265
+ /// Returns a reference to the underlying `String` if the object is unlinked.
246
266
pub fn as_str ( & self ) -> Option < & str > {
247
267
match self {
248
268
BytecodeObject :: Bytecode ( _) => None ,
249
269
BytecodeObject :: Unlinked ( s) => Some ( s. as_str ( ) ) ,
250
270
}
251
271
}
252
272
253
- /// Returns the unlinked `String` if the object is unlinked or empty
273
+ /// Returns the unlinked `String` if the object is unlinked.
254
274
pub fn into_unlinked ( self ) -> Option < String > {
255
275
match self {
256
276
BytecodeObject :: Bytecode ( _) => None ,
257
277
BytecodeObject :: Unlinked ( code) => Some ( code) ,
258
278
}
259
279
}
260
280
261
- /// Whether this object is still unlinked
281
+ /// Whether this object is still unlinked.
262
282
pub fn is_unlinked ( & self ) -> bool {
263
283
matches ! ( self , BytecodeObject :: Unlinked ( _) )
264
284
}
265
285
266
- /// Whether this object a valid bytecode
286
+ /// Whether this object a valid bytecode.
267
287
pub fn is_bytecode ( & self ) -> bool {
268
288
matches ! ( self , BytecodeObject :: Bytecode ( _) )
269
289
}
270
290
271
291
/// Returns `true` if the object is a valid bytecode and not empty.
272
- /// Returns false the object is a valid but empty bytecode or unlinked.
292
+ ///
293
+ /// Returns `false` if the object is a valid but empty or unlinked bytecode.
273
294
pub fn is_non_empty_bytecode ( & self ) -> bool {
274
295
self . as_bytes ( ) . map ( |c| !c. 0 . is_empty ( ) ) . unwrap_or_default ( )
275
296
}
276
297
277
- /// Tries to resolve the unlinked string object a valid bytecode object in place
298
+ /// Tries to resolve the unlinked string object a valid bytecode object in place.
278
299
///
279
300
/// Returns the string if it is a valid
280
301
pub fn resolve ( & mut self ) -> Option < & Bytes > {
@@ -286,7 +307,8 @@ impl BytecodeObject {
286
307
self . as_bytes ( )
287
308
}
288
309
289
- /// Link using the fully qualified name of a library
310
+ /// Links using the fully qualified name of a library.
311
+ ///
290
312
/// The fully qualified library name is the path of its source file and the library name
291
313
/// separated by `:` like `file.sol:Math`
292
314
///
@@ -311,18 +333,19 @@ impl BytecodeObject {
311
333
self
312
334
}
313
335
314
- /// Link using the `file` and `library` names as fully qualified name `<file>:<library>`
315
- /// See `BytecodeObject::link_fully_qualified`
336
+ /// Links using the `file` and `library` names as fully qualified name `<file>:<library>`.
337
+ ///
338
+ /// See [`link_fully_qualified`](Self::link_fully_qualified).
316
339
pub fn link (
317
340
& mut self ,
318
341
file : impl AsRef < str > ,
319
342
library : impl AsRef < str > ,
320
343
addr : Address ,
321
344
) -> & mut Self {
322
- self . link_fully_qualified ( format ! ( "{}:{}" , file. as_ref( ) , library. as_ref( ) , ) , addr)
345
+ self . link_fully_qualified ( format ! ( "{}:{}" , file. as_ref( ) , library. as_ref( ) ) , addr)
323
346
}
324
347
325
- /// Links the bytecode object with all provided `(file, lib, addr)`
348
+ /// Links the bytecode object with all provided `(file, lib, addr)`.
326
349
pub fn link_all < I , S , T > ( & mut self , libs : I ) -> & mut Self
327
350
where
328
351
I : IntoIterator < Item = ( S , T , Address ) > ,
@@ -335,7 +358,7 @@ impl BytecodeObject {
335
358
self
336
359
}
337
360
338
- /// Whether the bytecode contains a matching placeholder using the qualified name
361
+ /// Returns whether the bytecode contains a matching placeholder using the qualified name.
339
362
pub fn contains_fully_qualified_placeholder ( & self , name : impl AsRef < str > ) -> bool {
340
363
if let BytecodeObject :: Unlinked ( unlinked) = self {
341
364
let name = name. as_ref ( ) ;
@@ -346,7 +369,7 @@ impl BytecodeObject {
346
369
}
347
370
}
348
371
349
- /// Whether the bytecode contains a matching placeholder
372
+ /// Returns whether the bytecode contains a matching placeholder.
350
373
pub fn contains_placeholder ( & self , file : impl AsRef < str > , library : impl AsRef < str > ) -> bool {
351
374
self . contains_fully_qualified_placeholder ( format ! ( "{}:{}" , file. as_ref( ) , library. as_ref( ) ) )
352
375
}
@@ -400,9 +423,14 @@ pub struct DeployedBytecode {
400
423
}
401
424
402
425
impl DeployedBytecode {
403
- /// Returns the underlying `Bytes` if the object is a valid bytecode, and not empty
426
+ /// Returns a reference to the underlying `Bytes` if the object is a valid bytecode.
427
+ pub fn bytes ( & self ) -> Option < & Bytes > {
428
+ self . bytecode . as_ref ( ) . and_then ( |bytecode| bytecode. object . as_bytes ( ) )
429
+ }
430
+
431
+ /// Returns the underlying `Bytes` if the object is a valid bytecode.
404
432
pub fn into_bytes ( self ) -> Option < Bytes > {
405
- self . bytecode ? . object . into_bytes ( )
433
+ self . bytecode . and_then ( |bytecode| bytecode . object . into_bytes ( ) )
406
434
}
407
435
}
408
436
@@ -432,6 +460,16 @@ impl CompactDeployedBytecode {
432
460
Self { bytecode : Some ( CompactBytecode :: empty ( ) ) , immutable_references : Default :: default ( ) }
433
461
}
434
462
463
+ /// Returns a reference to the underlying `Bytes` if the object is a valid bytecode.
464
+ pub fn bytes ( & self ) -> Option < & Bytes > {
465
+ self . bytecode . as_ref ( ) . and_then ( |bytecode| bytecode. object . as_bytes ( ) )
466
+ }
467
+
468
+ /// Returns the underlying `Bytes` if the object is a valid bytecode.
469
+ pub fn into_bytes ( self ) -> Option < Bytes > {
470
+ self . bytecode . and_then ( |bytecode| bytecode. object . into_bytes ( ) )
471
+ }
472
+
435
473
/// Returns the parsed source map
436
474
///
437
475
/// See also <https://docs.soliditylang.org/en/v0.8.10/internals/source_mappings.html>
0 commit comments