| Crates.io | for-sure |
| lib.rs | for-sure |
| version | 0.1.2 |
| created_at | 2025-07-14 15:59:35.003602+00 |
| updated_at | 2025-09-23 18:24:24.530297+00 |
| description | Option-like type but with Deref implementation |
| homepage | |
| repository | https://www.github.com/hack3rmann/for-sure |
| max_upload_size | |
| id | 1751990 |
| size | 47,019 |
Almost typeSometimes you need the optional type which is almost always is Some.
The Almost type provides such functionality. It implements both
Deref<Target = T> and DerefMut which panic if the value is uninitilized.
It is actually really bad to use Almost instead of Option because it
disables 'null-safety' of Rust. But in some use cases it may help a lot.
For example, structs with two-factor inititalization:
# struct Window { pub name: String }
# impl Window {
# pub fn new(name: &str) -> Self {
# Self { name: name.to_owned() }
# }
# }
#
# trait App {
# fn open_window(&mut self, name: &str);
# fn update(&mut self);
# }
use for_sure::prelude::*;
// first init factor writes `Nil` to `window`
#[derive(Default)]
struct MyApp {
window: Almost<Window>,
}
// trait implementation which initializes `MyApp`
impl App for MyApp {
// this function calls always when `MyApp` is initialized,
// but we forced to use option-like types
fn open_window(&mut self, name: &str) {
// the second factor initializes window completely
self.window = Value(Window::new(name));
}
fn update(&mut self) {
// you can access window's methods (or fields) without
// writing `.as_ref().unwrap()` everywhere
println!("name: {}", self.window.name);
}
}
Note that it will panic if Almost's value accessed uninitilized.
std - enables std library.