https://github.com/aweirddev/niceowner
A stupid library that allows you to own a value, even if it comes from a reference. No cloning.
https://github.com/aweirddev/niceowner
borrowing ownership rust rust-ownership
Last synced: 3 months ago
JSON representation
A stupid library that allows you to own a value, even if it comes from a reference. No cloning.
- Host: GitHub
- URL: https://github.com/aweirddev/niceowner
- Owner: AWeirdDev
- Created: 2025-01-05T14:53:18.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-05T14:58:35.000Z (about 1 year ago)
- Last Synced: 2025-02-06T12:16:40.406Z (11 months ago)
- Topics: borrowing, ownership, rust, rust-ownership
- Language: Rust
- Homepage: https://crates.io/crates/niceowner
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# niceowner
A stupid library that allows you to own a value, even if it comes from a reference. No cloning.
You can use this with an `Rc` or `Arc`, if you're interested in cloning the reference of `NiceOwner`.
## Usage
```rust
use niceowner::NiceOwner;
struct Dog {
name: String,
}
fn lend_to_amy(no: &mut NiceOwner) {
// The dog is essentially *borrowed*.
// But now Amy is going to own it... somehow.
let mut dog = no.own().unwrap();
dog.name = String::from("Amy's dog");
// Amy remembers to return the dog back to the owner.
no.return_value(dog);
}
fn main() {
let dog = Dog { name: String::from("Dee'O G") };
let mut owned_dog = NiceOwner::new(dog);
// The owner is going abroad, so let's ask Amy to take care of the dog.
lend_to_amy(&mut owned_dog);
// The owner sees the nametag of the dog.
println!("The name of the dog is {:?}", owned_dog.name); // "Amy's dog"
// The dog now obeys Amy! :O
}
```
***
(c) 2025 AWeirdDev