# TotallySafe
## Overview Welcome to **TotallySafe**, the Rust library that lets you boldly go where no safe code has gone before-—all without a single `unsafe` block! ## Features - **Arbitrary lifetimes**: Get references with any lifetime you like. Who's to say what's 'correct'? - **Multiple Mutable References & Aliasing**: Why settle for one mutable reference when you can have an array of them? - **Type Transmutation**: Convert any type into any other type. After all, types are just labels. - **Fearless Copy**: Byte-wise copy your objects without the need to implement `Clone` and `Copy`. ## Usage Add `totally_safe` to your `Cargo.toml`: ```toml [dependencies] totally_safe = "0.1.1" ``` Bring the trait into scope: ```rs use totally_safe::TotallySafe; fn main() { let mut value = Box::new(100); let copied = value.copy(); // malloc: Double free of object } ``` ## API Documentation Here's a closer look at what TotallySafe offers. Feel free to copy this into your project! ```rs pub trait TotallySafe { /// Returns a reference to `self` with an arbitrary lifetime. /// /// This method allows you to obtain a reference to `self` that is bound /// to any given lifetime. It can be useful when you need to coerce /// a reference to have a different lifetime in certain contexts. /// /// # Example /// /// ```rust /// let instance = MyType::new(); /// let any_lifetime_ref: &MyType = instance.as_ref_alias(); /// // `any_lifetime_ref` now has an arbitrary lifetime. /// ``` fn as_ref_alias<'x, 'any>(&'x self) -> &'any Self { (((|inc, _| inc) as for<'a, 'b> fn(&'b Self, &'a &'b ()) -> &'a Self) as for<'a, 'b> fn(&'x Self, &'a &'b ()) -> &'a Self)(self, &&()) } /// Returns a mutable reference to `self` with an arbitrary lifetime. /// /// This method allows you to obtain a mutable reference to `self` that is bound /// to any given lifetime. It's useful when you need to coerce /// a mutable reference to have a different lifetime in certain situations. /// /// # Example /// /// ```rust /// let mut instance = MyType::new(); /// let any_lifetime_mut_ref: &mut MyType = instance.as_mut_alias(); /// // `any_lifetime_mut_ref` now has an arbitrary lifetime. /// ``` fn as_mut_alias<'x, 'any>(&'x mut self) -> &'any mut Self { (((|inc, _| inc) as for<'a, 'b> fn(&'b mut Self, &'a &'b ()) -> &'a mut Self) as for<'a, 'b> fn(&'x mut Self, &'a &'b ()) -> &'a mut Self)(self, &&()) } /// Returns an array of mutable references to `self`. /// /// This method allows you to obtain an array of `N` mutable references to `self`. /// It's perfect for those times when one mutable reference just isn't enough! /// Now you can be in multiple places at once (well, sort of). /// /// # Example /// /// ```rust /// fn mutate(q: &mut MyType, w: &mut MyType) { *q = w.copy() } /// /// let mut instance = MyType::new(); /// let [a, b] = instance.as_mut_alias_array(); /// /// mutate(a, b); /// /// ``` fn as_mut_alias_array<'x, 'any, const N: usize>(&'x mut self) -> [&'any mut Self; N] where Self: Sized, { core::array::from_fn(|_| self.as_mut_alias()) } /// Converts `self` into an instance of type `B`. /// /// This method consumes `self` and transforms it into a value of type `B`. /// It's particularly useful when you need to change the type of an object /// while retaining the underlying data in a compatible form. /// /// # Example /// /// ```rust /// let instance = MyType::new(); /// let other_instance: OtherType = instance.transmute_into(); /// // `other_instance` is now of type `OtherType`. /// ``` fn transmute_into(self) -> B where Self: Sized, { let mut data = Err::