Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cell does not require Copy types #6

Closed
juhdanad opened this issue Aug 24, 2020 · 3 comments
Closed

Cell does not require Copy types #6

juhdanad opened this issue Aug 24, 2020 · 3 comments
Assignees
Labels
bug Something isn't working Correctness Suggestion
Milestone

Comments

@juhdanad
Copy link

The infographic suggests that Cell can only be used with Copy types. But you can move anything into a Cell. See the replace method.

@usagi
Copy link
Owner

usagi commented Aug 24, 2020

Hmm, yes, you are right. I'll add a note or modify the related cheat in the next "rev.2". Thank you for your suggestion.

// eg.
let cell = Cell::new(vec![1,2,3]);
let original = cell.replace(vec![4,5,6]);
assert_eq!( &original[..], [1,2,3] ); // ok

@usagi usagi added this to the rev.2 milestone Aug 24, 2020
@usagi usagi self-assigned this Aug 24, 2020
@usagi usagi added bug Something isn't working Correctness Suggestion labels Aug 24, 2020
@juhdanad
Copy link
Author

According to the documentation of the std::cell crate,

Cell types come in two flavors: Cell<T> and RefCell<T>. Cell<T> implements interior mutability by moving values in and out of the Cell<T>. To use references instead of values, one must use the RefCell<T> type, acquiring a write lock before mutating.

So I think the choice of Cell vs RefCell depends on whether you want plain & references to the contained data or not.

@usagi
Copy link
Owner

usagi commented Sep 23, 2020

Note: I'll plan to merge rev-2-preparing branch to master in the next weekend.

@usagi usagi closed this as completed in f373362 Sep 26, 2020
usagi added a commit that referenced this issue Sep 26, 2020
fix #6, readme typos and rev.2 metadata updates
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working Correctness Suggestion
Projects
None yet
Development

No branches or pull requests

2 participants