fakeowned

Crates.iofakeowned
lib.rsfakeowned
version0.1.4
created_at2025-12-22 02:14:49.392665+00
updated_at2025-12-22 03:25:43.731076+00
descriptionUtility to convert from &Borrowed to &Owned <>
homepage
repositoryhttps://github.com/Nekrolm/fakeowned
max_upload_size
id1998914
size12,565
Dmitry Sviridkin (Nekrolm)

documentation

https://docs.rs/fakeowned/0.1.4/fakeowned/

README

FakeOwned

Have you ever wanted to store (String, String) as a key-pair in standard Hashmap/Btreemap, but suddenly realised that all lookups will require string allocations because of bad hashmap API?

Or may be you found a great library which solves all your problems but some misguided fool used &String instead of &str in their public API and now you have to allocate strings?

This crate provides A SOLUTION

BEHOLD! [FakeOwned] !

Now you can safely go

&str -> &String

&[T] -> &Vec<T>

(&str, &str) -> &(String, String)

[&str; N] -> &[String; N]

and any other combinations.

Without copies and nasty allocations.

But beware! DON'T USE IT UNLESS YOU ARE DESPERATE ENOUGH

use std::{collections::HashMap, ops::Deref};
use fakeowned::FakeOwned;
let map = HashMap::<(String, String), u32>::from_iter(
       [(("key1".to_string(), "key2".to_string()), 42)]
);
// This doesn't work: expected &(String, String)
// map.get(("key1", "key2"));
// You have to allocate strings. That's sad
let x = map.get(&("key1".to_string(), "key2".to_string()));
assert_eq!(x, Some(&42));
// This works without string allocations
let x = map.get(&("key1", "key2").fake_owned());
assert_eq!(x, Some(&42));

This crate highly relies on the fact that unsafe functions:

  • Vec::from_raw_parts
  • String::from_raw_parts Don't cause any nonsence, if the returned value immediatelly wrapped into [std::mem::ManuallyDrop]
Commit count: 0

cargo fmt