diff options
| author | Philipp Krones <hello@philkrones.com> | 2022-09-09 13:36:26 +0200 |
|---|---|---|
| committer | Philipp Krones <hello@philkrones.com> | 2022-09-09 13:36:26 +0200 |
| commit | 98bf99e2f8cf8b357d63a67ce67d5fc5ceef8b3c (patch) | |
| tree | 9737ff22b257f29282e7538d9ecb264451a3c1c0 /src/docs/rc_buffer.txt | |
| parent | 854f751b263dfac06dc3f635f8a9f92b8bc51da6 (diff) | |
| download | rust-98bf99e2f8cf8b357d63a67ce67d5fc5ceef8b3c.tar.gz rust-98bf99e2f8cf8b357d63a67ce67d5fc5ceef8b3c.zip | |
Merge commit 'b52fb5234cd7c11ecfae51897a6f7fa52e8777fc' into clippyup
Diffstat (limited to 'src/docs/rc_buffer.txt')
| -rw-r--r-- | src/docs/rc_buffer.txt | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/docs/rc_buffer.txt b/src/docs/rc_buffer.txt new file mode 100644 index 00000000000..82ac58eeb30 --- /dev/null +++ b/src/docs/rc_buffer.txt @@ -0,0 +1,27 @@ +### What it does +Checks for `Rc<T>` and `Arc<T>` when `T` is a mutable buffer type such as `String` or `Vec`. + +### Why is this bad? +Expressions such as `Rc<String>` usually have no advantage over `Rc<str>`, since +it is larger and involves an extra level of indirection, and doesn't implement `Borrow<str>`. + +While mutating a buffer type would still be possible with `Rc::get_mut()`, it only +works if there are no additional references yet, which usually defeats the purpose of +enclosing it in a shared ownership type. Instead, additionally wrapping the inner +type with an interior mutable container (such as `RefCell` or `Mutex`) would normally +be used. + +### Known problems +This pattern can be desirable to avoid the overhead of a `RefCell` or `Mutex` for +cases where mutation only happens before there are any additional references. + +### Example +``` +fn foo(interned: Rc<String>) { ... } +``` + +Better: + +``` +fn foo(interned: Rc<str>) { ... } +``` \ No newline at end of file |
