-
-
Save dhable/ff3ac1f7890170c7165544716d1df7c0 to your computer and use it in GitHub Desktop.
Bracket pattern in Rust
This file contains hidden or 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
The MIT License (MIT) | |
Copyright (c) 2020 Richard Cook | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or 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
/// See https://wiki.haskell.org/Bracket_pattern | |
/// # Arguments | |
/// | |
/// * `R` - type of resource | |
/// * `T` - type of return value from computation | |
/// * `E` - type of error | |
/// * `acquire` - possibly failing function that acquires resource | |
/// * `release` - function that releases resource | |
/// * `consume` - possibly failing function that consumes resource and yields return value of type `T` | |
#[allow(dead_code)] | |
pub fn bracket<R, T, E, F, G, H>(acquire: F, release: G, consume: H) -> std::result::Result<T, E> | |
where | |
F: FnOnce() -> std::result::Result<R, E>, | |
G: FnOnce(R) -> (), | |
H: FnOnce(&R) -> std::result::Result<T, E>, | |
{ | |
let resource = acquire()?; | |
let result = consume(&resource); | |
release(resource); | |
result | |
} |
This file contains hidden or 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
fn demo_bracket(acquire_resource: bool) -> std::io::Result<()> { | |
let result: &str = bracket::<_, _, std::io::Error, _, _, _>( | |
|| match acquire_resource { | |
true => Ok(Some(Resource::new())), | |
false => Ok(None), | |
}, | |
|resource| match resource { | |
Some(r) => r.release(), | |
None => {} | |
}, | |
|_| { | |
println!("Resource used"); | |
Err(std::io::Error::new( | |
std::io::ErrorKind::Other, | |
"Resource does not leak!", | |
)) | |
}, | |
)?; | |
println!("result={}", result); | |
Ok(()) | |
} |
This file contains hidden or 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
#[allow(unreachable_code)] | |
fn demo_drop(acquire_resource: bool) -> std::io::Result<()> { | |
struct ResourceHolder { | |
resource: Resource, | |
} | |
impl Drop for ResourceHolder { | |
fn drop(&mut self) { | |
self.resource.release() | |
} | |
} | |
let resource = match acquire_resource { | |
true => Some(Resource::new()), | |
false => None, | |
}; | |
let _holder = resource.map(|r| ResourceHolder { resource: r }); | |
println!("Resource used"); | |
return Err(std::io::Error::new( | |
std::io::ErrorKind::Other, | |
"Resource does not leak!", | |
)); | |
Ok(()) | |
} |
This file contains hidden or 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
#[allow(unreachable_code)] | |
#[allow(unused_variables)] | |
fn demo_explicit_drop(acquire_resource: bool) -> std::io::Result<()> { | |
struct ResourceHolder { | |
resource: Resource, | |
} | |
impl Drop for ResourceHolder { | |
fn drop(&mut self) { | |
self.resource.release() | |
} | |
} | |
let resource = match acquire_resource { | |
true => Some(Resource::new()), | |
false => None, | |
}; | |
let holder = resource.map(|r| ResourceHolder { resource: r }); | |
println!("Resource used"); | |
return Err(std::io::Error::new( | |
std::io::ErrorKind::Other, | |
"Resource does not leak!", | |
)); | |
drop(holder); | |
Ok(()) | |
} |
This file contains hidden or 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
#[allow(unreachable_code)] | |
#[allow(unused_variables)] | |
fn demo_resource_leak(acquire_resource: bool) -> std::io::Result<()> { | |
let resource = match acquire_resource { | |
true => Some(Resource::new()), | |
false => None, | |
}; | |
println!("Resource used"); | |
return Err(std::io::Error::new( | |
std::io::ErrorKind::Other, | |
"Leak the resource!", | |
)); | |
match resource { | |
Some(r) => r.release(), | |
None => {} | |
}; | |
Ok(()) | |
} |
This file contains hidden or 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
struct Resource {} | |
impl Resource { | |
fn new() -> Self { | |
println!("Resource acquired"); | |
Self {} | |
} | |
fn release(&self) { | |
println!("Resource released"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment