-
-
Save phaylon/013bfdc4802945bf19e69d5536dd3dc1 to your computer and use it in GitHub Desktop.
Match Ergonomics Issue A: T -> &mut T
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! fn_handler { | |
() => { | |
pub fn handle<'a>(value: Data<'a>) { | |
fn ffi_finalize(_ptr: *const i32) { | |
println!("finalizing ptr"); | |
} | |
fn ffi_reuse(_ptr: *const i32) { | |
println!("reusing ptr"); | |
} | |
match value.resource { | |
Some(resource) => { | |
println!("has resource {}", *value.some_ref); | |
let ptr = resource.some_ptr; | |
ffi_finalize(ptr); | |
// release other associated resources | |
drop(resource); | |
println!("assume related resource dropped"); | |
ffi_reuse(ptr); | |
}, | |
None => { | |
println!("no resource"); | |
}, | |
} | |
} | |
} | |
} | |
pub struct Resource { | |
some_ptr: *const i32, | |
} | |
impl Drop for Resource { | |
fn drop(&mut self) { | |
println!("releasing resources"); | |
} | |
} | |
mod owned { | |
use super::Resource; | |
pub struct Data<'a> { | |
some_ref: &'a i32, | |
resource: Option<Resource>, | |
} | |
pub fn test() { | |
let resource = Some(Resource { some_ptr: &23 as *const _ }); | |
handle(Data { | |
some_ref: &23, | |
resource, | |
}); | |
} | |
fn_handler!(); | |
} | |
mod mutref { | |
use super::Resource; | |
pub struct Data<'a> { | |
some_ref: &'a i32, | |
resource: &'a mut Option<Resource>, | |
} | |
pub fn test() { | |
let mut resource = Some(Resource { some_ptr: &23 as *const _ }); | |
handle(Data { | |
some_ref: &23, | |
resource: &mut resource, | |
}); | |
} | |
fn_handler!(); | |
} | |
fn main() { | |
println!("--- owned"); | |
owned::test(); | |
println!("--- mut ref"); | |
mutref::test(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment