Files
archy/core/target/debug/deps/libscopeguard-f245d02bea2a9ebf.rmeta
T

43 lines
21 KiB
Plaintext
Raw Normal View History

2026-01-24 22:59:20 +00:00
rust
~R#rustc 1.93.0 (254b59607 2026-01-19)Á³VOÐêùÿ²?ÑL¼§Ì±-a7a40eb546f9e2b0Áë&Gf·Ã ¬ëñq:¤»b-bb76018b1173caf6ÁOnUnwindÁDÀ6<6use_stdÁLš6œ6 OnSuccessÁLà7<°7¢Lº7œ°7defer_on_successÁ„§?<ô>¢Lþ>œô>defer_on_unwindÁ|²B<ÿA¢L‰BœÿAguard_on_successÁ„”[<áZ¢LëZœáZguard_on_unwindÁ|öb<Ãb¢LÍbœÃbtestsÁ,¤nà$™n³StrategyÁ 
should_runÁ
ðdeferÁ
ScopeGuardÁ˜dropfnÁstrategyÁ°¸
with_strategyÁ
into_innerÁguardÁ°  ° ¸$$°$¸$š$**°*¸*//°/¸/²44°4¸4ÍAlwaysÁ:Í)
.
3
8
8
8
;
;
;
 à`9i©©ìx ˜˜ÇÔ#Ze3ªáçç°¸ì½ù½ ½à`½:94
9 $*/à`# 
ìù  9³Í0ÍÜ0Á] PhantomDataÁ\ò0¯ Š ManuallyDropÁd1íÑ,ª1ìDerefMutÁD±1ù ÷ Å1ê
Œ,±<üò…¡= @ p8¹lA O7https://docs.rs/scopeguard/1/ÁüQü:8ütEB A scope guard will run a given closure when it goes out of scope,Áüº$! even if the code between panics.Áüß$! (as long as panic doesn't abort)ÁËtˆ # ExamplesÁË”› ## Hello WorldÁ®Ëü²@= This example creates a scope guard with an example function:ÁóË ```Áäÿ extern crate scopeguard;ÁœËd  fn f() {Áü­0- let _guard = scopeguard::guard((), |_| {ÁüÞ*' println!("Hello Scope Exit!");Á\‰ });ÁËü™! // rest of the code here.Á»Ëü¿OL // Here, at the end of `_guard`'s scope, the guard's closure is called.ÁüMJ // It is also called if we exit this scope through unwinding instead.ÁŒã # fn main() {Á
# f();Á # }Á<û
Ë|— ## `defer!`Á§Ëü«<9 Use the `defer` macro to run an operation at scope exit,Áüè?< either regular scope exit or during unwinding from a panic.Á¨Ëû
ü´0- #[macro_use(defer)] extern crate scopeguard;ÁåËÄé use std::cell::Cell;ÁË|† fn main() {Áü–QN // use a cell to observe drops during and after the scope guard is activeÁüè(% let drop_counter = Cell::new(0);ÁL ü› HE // Create a scope guard using `defer!` for the current scopeÁ¤ä  defer! {Áüù 96 drop_counter.set(1 + drop_counter.get());Á
Á
蟁
:7 // Do regular operations here in the meantime.Á Ëü„ 96 // Just before scope exit: it hasn't run yet.Áü¾ .+ assert_eq!(drop_counter.get(), 0);Áí Ëüñ KH // The following scope end is where the defer closure is calledÁ üÇ *' assert_eq!(drop_counter.get(), 1);Á º û

Ëì„
 ## Scope Guard with ValueÁ¢
Ëü¦
JG If the scope guard closure needs to access an outer value that is alsoÁüñ
PM mutated outside of the scope guard, then you may want to use the scope guardÁüÂNK with a value. The guard works like a smart pointer, so the inner value canÁü‘52 be accessed by reference or by mutable reference.ÁÇËüË  ### 1. The guard owns a fileÁìËüðOL In this example, the scope guard owns a file and ensures pending writes areÁÌÀ synced at scope exit.ÁÚËû
äæŠƒËœ‡ use std::fs::*;Áü› use std::io::{self, Write};Áü»96 # // Mock file so that we don't actually write a fileÁ´õ # struct MockFile;Á¬Œ # impl MockFile {Áü¢B? # fn create(_s: &str) -> io::Result<Self> { Ok(MockFile) }ÁüåEB # fn write_all(&self, _b: &[u8]) -> io::Result<()> { Ok(()) }Áü«96 # fn sync_all(&self) -> io::Result<()> { Ok(()) }Áõüí! # use self::MockFile as File;ÁËü“%" fn try_main() -> io::Result<()> {Áü¹-* let f = File::create("newfile.txt")?;Áüç1. let mut file = scopeguard::guard(f, |f| {Áü™63 // ensure we flush file at return or panicÁüÐ! let _ = f.sync_all();Á¹üþ96 // Access the file through the scope guard itselfÁü¸0- file.write_all(b"test me\n").map(|_| ())ÁºïËÎäƒ try_main().unwrap();Á, º¦Ëû
²Ëü¶85 ### 2. The guard restores an invariant on scope exitÁïËû
äûŠ˜Ëüœ use std::mem::ManuallyDrop;ÁŒ¼ use std::ptr;ÁÎËüÒDA // This function, just for this example, takes the first elementÁü—A> // and inserts it into the assumed sorted tail of the vector.Á //ÁüàKH // For optimization purposes we temporarily violate an invariant of theÁü¬-* // Vec, that it owns all of its elements.ÁÙ)üáJG // The safe approach is to use swap, which means two writes to memory,Áü¬RO // the optimization is to use a “hole†which uses only one write of memoryÁüÿ" // for each position it moves.ÁÙ)ü©>; // We *must* use a scope guard to run this code safely. WeÁüèMJ // are running arbitrary user code (comparison operators) that may panic.Áü¶HE // The scope guard ensures we restore the invariant after successfulÁüÿ+( // exit or during unwinding from panic.Áü«.+ fn insertion_sort_first<T>(v: &mut Vec<T>)ÁÜÚ where T: PartialOrdÁüü  struct Hole<'a, T: 'a> {Áô v: &'a mut Vec<T>,Á̼ index: usize,ÁüÖ# value: ManuallyDrop<T>,ÁöË„ˆ
unsafe {Áü™ HE // Create a moved-from location in the vector, a “holeâ€üâ )& let value = ptr::read(&v[0]);ÁüŒ!TQ let mut hole = Hole { v: v, index: 0, value: ManuallyDrop::new(value) };Áá!Ëüå!.+ // Use a scope guard with a value.Áü”"GD // At scope exit, plug the hole so that the vector is fullyÁüÜ"! // initialized again.Áüþ"UR // The scope guard owns the hole, but we can access it through the guard.ÁüÔ#A> let mut hole_guard = scopeguard::guard(hole, |hole| {Áü–$SP // plug the hole in the vector with the value that was // taken outÁüê$'$ let index = hole.index;Áü’%NK ptr::copy_nonoverlapping(&*hole.value, &mut hole.v[index],
ù ÷ 0ê
ŒCĸ0ŒD×0žÄå0¼´ƒ1îÜŸ1DÀ10”‰2üË1=: Controls in which cases the associated code should be runÁD“2  ªâü‰2« æGæG  Äš3ü¢2=: Return `true` if the guard’s associated code should runÁüä21. (in the context where this method is called).ÁT3 
îGÄî7 ¼£8T¦8

”¤<üó:0- Macro to create a `ScopeGuard` (always run).Á¤;Ëü¨;?< The macro takes statements, which are the body of a closureÁüè;+( that will run when the scope is exited.Á&|”<C ·< Ž= ½< Ç<, ¾< ¿< Å<, À<8 Á<& Â<8Ã<
Æ<*É< Ì< = 8Ö<8_guardÁ4Ú< á<, ã<8,ä<'é<8¡,ë< ð< „= ñ< ò<$ ó< õ< ö< ÷< ø< ú< ƒ=, ü< ý< €=, þ<8œK ÿ<