Skip to content

Instantly share code, notes, and snippets.

@eddyb
Created September 28, 2015 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddyb/6c58b033b7621f436f47 to your computer and use it in GitHub Desktop.
Save eddyb/6c58b033b7621f436f47 to your computer and use it in GitHub Desktop.
enum Option<T> {
None,
Some(T)
}
// desugars to
enum Option<T> {
None {..},
Some {..}
}
struct Option<T>::None;
struct Option<T>::Some(T);
enum Mode {
enum ByRef {
Mutable,
Immutable
}
ByValue
}
// desugars to
enum Mode {
enum ByRef {..}
ByValue {..}
}
enum Mode::ByRef {
Mutable {..},
Immutable {..}
}
struct Mode::ByValue;
struct Mode::ByRef::Mutable;
struct Mode::ByRef::Immutable;
type Ty<'tcx> = &'tcx TypeData<'tcx>;
enum TypeData<'tcx> {
// Common fields:
id: u32,
flags: u32,
...,
// Variants:
Int { },
Uint { },
Ref { referent_ty: Ty<'tcx> },
...
}
// desugars to
enum TypeData<'tcx> {
// Common fields:
id: u32,
flags: u32,
...,
// Variants:
Int {..},
Uint {..},
Ref {..},
...
}
struct TypeData<'tcx>::Int {}
struct TypeData<'tcx>::Uint {}
struct TypeData<'tcx>::Ref {
referent_ty: Ty<'tcx>
}
type Ty<'tcx> = &'tcx TypeData<'tcx>;
enum TypeData<'tcx> {
...,
// No indirection anymore. What's more, the type `FnPointer`
// serves as a refinement type automatically.
FnPointer {
unsafety: Unsafety,
abi: Abi,
signature: Signature,
}
}
// This prevents mutation of the tag after "unsizing" from
// variant to enum type (&TypeData::FnPointer -> &TypeData),
// so the size of each variant can be smaller than the enum.
// It also may allow individual variants to be unsized, but
// that could be odd to deal with.
impl !Sized for TypeData {}
enum Node {
// where this node is positioned after layout
position: Rectangle,
...
enum Element {..}
}
impl !Sized for Node {}
enum Node::Element {
...
// These could be simply named Text and Paragraph.
TextElement {..},
ParagraphElement {..}
}
struct Node::Element::TextElement {
...
}
struct Node::Element::ParagraphElement {
...
}
// Alternatively, T: Unsize<Node> could be used.
fn intersects<T: VariantOf<Node>>(box: Rectangle, elements: &[Rc<T>]) -> Vec<Rc<T>> {
...
}
trait TextAPIs: VariantOf<HTMLElement> {
fn maxLength(&self) -> usize;
..
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment