https://github.com/bincode-org/unty
A crate that allows you to mostly-safely cast one type into another type.
https://github.com/bincode-org/unty
Last synced: about 1 year ago
JSON representation
A crate that allows you to mostly-safely cast one type into another type.
- Host: GitHub
- URL: https://github.com/bincode-org/unty
- Owner: bincode-org
- License: apache-2.0
- Created: 2023-09-23T13:23:11.000Z (over 2 years ago)
- Default Branch: trunk
- Last Pushed: 2025-03-07T08:08:46.000Z (over 1 year ago)
- Last Synced: 2025-04-09T11:09:49.389Z (about 1 year ago)
- Language: Rust
- Homepage:
- Size: 18.6 KB
- Stars: 5
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
A crate that allows you to mostly-safely cast one type into another type.
This is mostly useful for generic functions, e.g.
```rs
pub fn foo(s: S) {
if let Ok(a) = unsafe { unty::(s) } {
println!("It is an u8 with value {a}");
} else {
println!("it is not an u8");
}
}
foo(10u8); // will print "it is an u8"
foo("test"); // will print "it is not an u8"
```
This operation is still unsafe because it allows you to extend lifetimes. There currently is not a way to prevent this
```rs
if let Ok(str) = unsafe { unty::<&'a str, &'static str>(input) } {
// the compiler may now light your PC on fire
}
```
# License
This crate is dual licenced MIT and Apache-2.0, at your own leisure