Skip to content

Instantly share code, notes, and snippets.

@LunNova
Created June 24, 2025 16:37
Show Gist options
  • Save LunNova/7acb908d3390ecc2f8f4f52dc1496d24 to your computer and use it in GitHub Desktop.
Save LunNova/7acb908d3390ecc2f8f4f52dc1496d24 to your computer and use it in GitHub Desktop.
// Claude Code found this file hanging out in a project and had this to say:
// FIXME: This entire file is cursed unsafe wizardry that needs investigation!
// - Uses very unsafe transmute tricks with MaybeUninit that might be UB
// - Has two different make_owned implementations for unclear reasons
// - Liberal use of unreachable_unchecked()
// - Unclear if this is even used anywhere or just abandoned experimental code
// - The replace_with() function looks particularly unsound
// Rest of the file sadly by past!LunNova. I don't remember what I was trying to do and it seems like a mess.
// ... we already have CoW why did I make this
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use std::{hint, mem};
#[repr(transparent)]
pub struct LazyCopy<T: Clone>(Inner<T>);
#[repr(u8)]
enum Inner<T> {
Owned(T),
Shared(Arc<T>),
}
unsafe fn always_unsafe() {}
macro_rules! unsafe_unreachable {
() => {{
always_unsafe();
#[cfg(debug_assertions)]
{
unreachable!("unsafe_unreachable hit in debug build, you fucked up")
}
#[cfg(not(debug_assertions))]
{
std::hint::unreachable_unchecked()
}
}};
}
fn make_owned<T: Clone>(lc: &mut Inner<T>) -> &mut T {
// if let Inner::Owned(ref mut owned) = lc {
// return owned;
// }
let inner = match lc {
Inner::Owned(ref mut owned) => return owned,
Inner::Shared(ref mut inner) => (**inner).clone(),
};
*lc = Inner::Owned(inner);
match lc {
Inner::Owned(ref mut a) => a,
_ => unsafe { unsafe_unreachable!() },
}
}
#[inline(always)]
fn replace_with<T>(x: &mut T, closure: impl Fn(T) -> T) {
let lc_maybe_uninit: &mut MaybeUninit<T> = unsafe { mem::transmute(x) };
let prev = mem::replace(lc_maybe_uninit, MaybeUninit::uninit());
let next = closure(unsafe { prev.assume_init() });
let _ = mem::replace(lc_maybe_uninit, MaybeUninit::new(next));
}
fn make_owned_unsafe<T: Clone>(lc: &mut Inner<T>) -> &mut T {
replace_with(lc, |lc| match lc {
Inner::Owned(s) => Inner::Owned(s),
Inner::Shared(arc) => Inner::Owned((*arc).clone()),
});
match lc {
Inner::Owned(lc) => lc,
_ => unsafe { hint::unreachable_unchecked() },
}
}
impl<T: Clone> DerefMut for LazyCopy<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
make_owned_unsafe(&mut self.0)
}
}
impl<T: Clone> LazyCopy<T> {
pub fn share_and_copy(&mut self) -> LazyCopy<T> {
replace_with(&mut self.0, |x| match x {
Inner::Owned(a) => Inner::Shared(Arc::new(a)),
Inner::Shared(s) => Inner::Shared(s),
});
match self.0 {
Inner::Shared(ref mut s) => LazyCopy(Inner::Shared(s.clone())),
Inner::Owned(_) => unsafe { hint::unreachable_unchecked() },
}
}
pub fn from_owned(t: T) -> Self {
LazyCopy(Inner::Owned(t))
}
pub fn extract_owned(self) -> T {
match self.0 {
Inner::Owned(owned) => owned,
Inner::Shared(shared) => (*shared).clone(),
}
}
}
impl<T: Clone> Deref for LazyCopy<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
match &self.0 {
Inner::Owned(a) => a,
Inner::Shared(b) => b.deref(),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment