{"id":29902981,"url":"https://github.com/antouhou/anycow","last_synced_at":"2025-08-01T16:47:16.275Z","repository":{"id":306854932,"uuid":"1027302582","full_name":"antouhou/anycow","owner":"antouhou","description":"A supercharged container for read-heavy, occasionally-updated data structures with multiple storage strategies","archived":false,"fork":false,"pushed_at":"2025-07-28T03:42:23.000Z","size":29,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-28T03:49:06.213Z","etag":null,"topics":["data-container","data-structures","multithreading","rust","shared-ownership"],"latest_commit_sha":null,"homepage":"https://docs.rs/anycow","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antouhou.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-27T18:31:07.000Z","updated_at":"2025-07-28T03:41:23.000Z","dependencies_parsed_at":"2025-07-28T03:59:13.734Z","dependency_job_id":null,"html_url":"https://github.com/antouhou/anycow","commit_stats":null,"previous_names":["antouhou/anycow"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/antouhou/anycow","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antouhou%2Fanycow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antouhou%2Fanycow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antouhou%2Fanycow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antouhou%2Fanycow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antouhou","download_url":"https://codeload.github.com/antouhou/anycow/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antouhou%2Fanycow/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268264349,"owners_count":24222508,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-container","data-structures","multithreading","rust","shared-ownership"],"created_at":"2025-08-01T16:47:13.830Z","updated_at":"2025-08-01T16:47:16.224Z","avatar_url":"https://github.com/antouhou.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🐄 AnyCow\n\n[![Crates.io](https://img.shields.io/crates/v/anycow.svg)](https://crates.io/crates/anycow)\n[![Documentation](https://docs.rs/anycow/badge.svg)](https://docs.rs/anycow)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\n\u003e A supercharged container for read-heavy, occasionally-updated data structures\n\n**AnyCow** is a versatile, high-performance container that extends the concept of `Cow` (Clone-on-Write) with multiple storage strategies optimized for different use cases. Perfect for scenarios where you need to read values frequently but update them only occasionally.\n\n## 🚀 Features\n\n- **Multiple Storage Strategies**: Choose the right storage for your use case\n  - `Borrowed` - Zero-cost references for temporary data\n  - `Owned` - Heap-allocated owned data via `Box\u003cT\u003e`\n  - `Shared` - `Arc\u003cT\u003e` for shared immutable data\n  - `Updatable` - Lock-free atomic updates using `arc-swap`\n  - `Lazy` - Lazy initialization with atomic updates for static contexts\n\n- **Lock-Free Updates**: The `Updatable` and `Lazy` variants use `arc-swap` for atomic, lock-free updates\n- **Lazy Initialization**: The `Lazy` variant makes it possbile to create an `Updatable` in a static context\n- **Flexible API**: Easy conversion between different storage types\n- **Zero-Cost Abstractions**: Minimal overhead for common operations\n- **Thread-Safe**: Share data safely across threads with `Shared`, `Updatable`, and `Lazy` variants\n\n## 📦 Installation\n\nAdd this to your `Cargo.toml`:\n\n```toml\n[dependencies]\nanycow = \"0.1\"\n```\n\n## 🎯 Use Cases\n\nAnyCow shines in scenarios where you have:\n\n- **Configuration data** that's read frequently but updated occasionally\n- **Global/static data** that needs lazy initialization and atomic updates\n- **Cached values** that need atomic updates without locks\n- **Shared state** across multiple threads with infrequent modifications\n- **Hot paths** where you want to minimize allocation overhead\n- **APIs** that need to accept both borrowed and owned data flexibly\n\n## 🔥 Quick Start\n\n```rust\nuse anycow::AnyCow;\n\n// Create from different sources\nlet borrowed = AnyCow::borrowed(\u0026\"hello\");\nlet owned = AnyCow::owned(String::from(\"world\"));\nlet shared = AnyCow::shared(std::sync::Arc::new(42));\nlet updatable = AnyCow::updatable(vec![1, 2, 3]);\nlet lazy = AnyCow::lazy(|| vec![7, 8, 9]);\n\n// Read values efficiently\nprintln!(\"{}\", *borrowed.borrow()); // \"hello\"\nprintln!(\"{}\", *owned.borrow());    // \"world\"\n\n// Atomic updates (lock-free!)\nupdatable.try_replace(vec![4, 5, 6]).unwrap();\nlazy.try_replace(vec![10, 11, 12]).unwrap();\n```\n\n## 💡 Examples\n\n### Reading Configuration\n\n```rust\nuse anycow::AnyCow;\nuse std::sync::Arc;\n\n#[derive(Clone, Debug)]\nstruct Config {\n    max_connections: usize,\n    timeout_ms: u64,\n}\n\n// Create updatable config\nlet config = AnyCow::updatable(Config {\n    max_connections: 100,\n    timeout_ms: 5000,\n});\n\n// Read frequently (very fast)\nlet current_config = config.borrow();\nprintln!(\"Max connections: {}\", current_config.max_connections);\n\n// Update occasionally (atomic, lock-free)\nconfig.try_replace(Config {\n    max_connections: 200,\n    timeout_ms: 3000,\n}).unwrap();\n```\n\n### Flexible API Design\n\n```rust\nuse anycow::AnyCow;\n\nfn process_data\u003c'a\u003e(data: AnyCow\u003c'a, str\u003e) {\n    // Works with borrowed, owned, or shared data\n    println!(\"Processing: {}\", *data.borrow());\n}\n\n// All of these work!\nprocess_data(AnyCow::borrowed(\"borrowed string\"));\nprocess_data(AnyCow::owned(String::from(\"owned string\")));\nprocess_data(AnyCow::shared(std::sync::Arc::new(String::from(\"shared string\"))));\n```\n\n### Cache with Atomic Updates\n\n```rust\nuse anycow::AnyCow;\nuse std::thread;\nuse std::sync::Arc;\n\nlet cache = Arc::new(AnyCow::updatable(vec![1, 2, 3]));\n\n// Spawn reader threads\nlet cache_clone = cache.clone();\nlet reader = thread::spawn(move || {\n    for _ in 0..1000 {\n        let data = cache_clone.borrow();\n        println!(\"Sum: {}\", data.iter().sum::\u003ci32\u003e());\n    }\n});\n\n// Update cache atomically\ncache.try_replace(vec![4, 5, 6, 7, 8]).unwrap();\n\nreader.join().unwrap();\n```\n\n### Lazy Global Configuration\n\n```rust\nuse anycow::AnyCow;\nuse std::collections::HashMap;\n\n// Perfect for static/global data that's expensive to initialize\nstatic CONFIG: AnyCow\u003cHashMap\u003cString, String\u003e\u003e = AnyCow::lazy(|| {\n    println!(\"Loading configuration...\"); // Only runs once!\n    let mut config = HashMap::new();\n    config.insert(\"app_name\".to_string(), \"MyApp\".to_string());\n    config.insert(\"version\".to_string(), \"1.0.0\".to_string());\n    config\n});\n\nfn main() {\n    // First access initializes the config\n    let app_name = CONFIG.borrow().get(\"app_name\").cloned().unwrap();\n    println!(\"App: {}\", app_name);\n    \n    // Subsequent accesses are fast (no re-initialization)\n    let version = CONFIG.borrow().get(\"version\").cloned().unwrap();\n    println!(\"Version: {}\", version);\n    \n    // Update the global config atomically\n    let mut new_config = HashMap::new();\n    new_config.insert(\"app_name\".to_string(), \"MyApp Pro\".to_string());\n    new_config.insert(\"version\".to_string(), \"2.0.0\".to_string());\n    CONFIG.try_replace(new_config).unwrap();\n}\n\n## 🧠 Storage Strategy Guide\n\n| Variant | Best For | Thread Safe | Mutable | Memory |\n|---------|----------|-------------|---------|--------|\n| `Borrowed` | Temporary refs, hot paths | ❌ | ❌ | Zero-copy |\n| `Owned` | Exclusive ownership | ❌ | ✅ | Heap |\n| `Shared` | Read-only sharing | ✅ | ❌ | Shared |\n| `Updatable` | Concurrent reads + atomic updates | ✅ | Via `try_replace()` | Shared + Atomic |\n| `Lazy` | Static/global data + atomic updates | ✅ | Via `try_replace()` | Lazy + Shared + Atomic |\n\n## 🔧 API Reference\n\n### Construction\n```rust\nAnyCow::borrowed(\u0026value)    // From reference\nAnyCow::owned(value)        // From owned value (boxed)\nAnyCow::shared(arc)         // From Arc\u003cT\u003e\nAnyCow::updatable(value)    // Create updatable variant\nAnyCow::lazy(init_fn)       // Create lazy variant with init function\n```\n\n### Access\n```rust\ncontainer.borrow()          // Get reference to value\ncontainer.to_mut()          // Get mutable reference (COW)\ncontainer.into_owned()      // Convert to owned value\ncontainer.to_arc()          // Convert to Arc\u003cT\u003e\n```\n\n### Updates\n```rust\ncontainer.try_replace(new_value)  // Atomic update (Updatable \u0026 Lazy only)\n```\n\n## ⚡ Performance\n\nAnyCow is designed for performance:\n\n- **Zero-cost borrowing**: No allocation for `Borrowed` variant\n- **Lazy initialization**: `Lazy` variant allow to create an `Updatable` in a static context\n- **Lock-free updates**: `Updatable` and `Lazy` use `arc-swap` for atomic operations\n- **Minimal overhead**: Smart enum design with efficient memory layout\n- **Branch prediction friendly**: Common operations are optimized\n\n## 🤝 Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.\n\n## 📄 License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## 🙏 Acknowledgments\n\n- Built with [`arc-swap`](https://crates.io/crates/arc-swap) for lock-free atomic operations\n- Inspired by the standard library's `Cow` but supercharged for modern use cases\n\n---\n\nMade with ❤️ for the Rust community. Happy coding! 🦀\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantouhou%2Fanycow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantouhou%2Fanycow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantouhou%2Fanycow/lists"}