4

what if pointer properties were in the rust type system

 1 year ago
source link: https://cohost.org/myrrlyn/post/197200-what-if-pointer-prop
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

would that be fucked up or what

so as you may or mayn't know, i am the author of the funty crate, which re-unifies all the primitives under a trait hierarchy so you can be generic over them, and i also experimentally am working on plugging pointer permissions into the type system with a module in bitvec.

incidentally this is what is colloquially known as "terminal library author brain", not as in "author of libraries for the terminal" but as in "incurable". if i ever learn haskel it is Over for you bitches

anyway

rust 1.65 has Generic Associated Types

which means i get to do incredibly brain genious shit like this:

trait Permission {
  type Raw<T: ?Sized>: Copy;
}

struct Shared; struct Unique;

impl Permission for Shared {
  type Raw<T: ?Sized> = *const T;
}
impl Permission for Unique {
  type Raw<T: ?Sized> = *mut T;
}
// this is a surprise tool that will help us later :)
impl Permission for (Shared, Unique) {
  type Raw<T: ?Sized> = <Shared as Permission>::Raw;
}

and not have to make Permission, or worse, Shared and Unique, generic over the type of the referent. cool, right? but wait. check this shit out:

trait Permission {
  // snip
  type Const: Permission;

  fn cast_const<T: ?Sized>(ptr: Self::Raw<T>) -> <Self::Const as Permission>::Raw<T>;
}

impl Permission for Shared {
  // snip

  type Const = Self;

  fn cast_const<T: ?Sized>(ptr: *const T) -> *const T { ptr }
}
impl Permission for for Unique {
  // snip

  type Const = (Shared, Self); // see? i told you

  fn cast_const<T: ?Sized>(ptr: *mut T) -> *const T { ptr.cast_const() }
}
impl Permission for (Shared, Unique) {
  // snip

  type Const = Self;

  fn cast_const<T: ?Sized>(ptr: *const T) -> *const T { ptr }
}

you may be thinking "alex what the fuck is this shit" which is fair. the fuck that this shit is, is that it's a type-system graph that allows you to at compile time prove that you can take a mutable pointer and dynamically degrade it to a const pointer and later re-upgrade it to mutable while completely disallowing upgrading const-only pointers to mutable. i'm not showing you a (Shared, Unique) -> Unique phase change because that's boring and you get the idea, but, yeah. if i don't define a (Shared) -> Unique phase change then you can never sneak mutability into a pointer that doesn't have it.

people keep telling me that the rust generic system is turing complete which means i am going to port miri to it

because i am fucking insane


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK