leptos/oco/README.md

32 lines
804 B
Markdown
Raw Normal View History

2024-04-27 15:04:23 +00:00
This module contains the `Oco` (Owned Clones Once) smart pointer,
which is used to store immutable references to values.
This is useful for storing, for example, strings.
2024-04-27 15:04:23 +00:00
Imagine this as an alternative to [`Cow`] with an additional, reference-counted
branch.
2024-04-27 15:04:23 +00:00
```rust
use oco_ref::Oco;
use std::sync::Arc;
2024-04-27 15:04:23 +00:00
let static_str = "foo";
let arc_str: Arc<str> = "bar".into();
let owned_str: String = "baz".into();
2024-04-27 15:04:23 +00:00
fn uses_oco(value: impl Into<Oco<'static, str>>) {
let mut value = value.into();
2024-04-27 15:04:23 +00:00
// ensures that the value is either a reference, or reference-counted
// O(n) at worst
let clone1 = value.clone_inplace();
2024-04-27 15:04:23 +00:00
// these subsequent clones are O(1)
let clone2 = value.clone();
let clone3 = value.clone();
}
2024-04-27 15:04:23 +00:00
uses_oco(static_str);
uses_oco(arc_str);
uses_oco(owned_str);
```