Skip to content

Instantly share code, notes, and snippets.

View Centril's full-sized avatar
🏠
Working from home

Mazdak Farrokhzad Centril

🏠
Working from home
  • Sweden
View GitHub Profile

Proposal:

Add setjmp and longjmp with the following semantics:

pub type JmpBuf = <implementation-defined>;

/// Saves the current execution context into `env`. If `longjmp(env, val)` is used 
/// to restore execution at this call site, this function returns `val`. Otherwise, 
/// it returns `0`. 
trait App<Input>
where
Input: { request: http::Request }
{
type Output: Into<http::Response>;
fn call(self, input: Input) -> Self::Output;
}
struct AuthApp<T> {
// This test enumerates various cases of interest for partial
// [re]initialization of ADTs and tuples.
//
// See rust-lang/rust#21232, rust-lang/rust#54986, and rust-lang/rust#54987.
//
// All of tests in this file are expected to change from being
// rejected, at least under NLL (by rust-lang/rust#54986) to being
// **accepted** when rust-lang/rust#54987 is implemented.
// (That's why there are assertions in the code.)
//
@rust-play
rust-play / playground.rs
Created August 30, 2018 00:21
Code shared from the Rust Playground
#![feature(unboxed_closures)]
#![feature(fn_traits)]
use std::marker::PhantomData;
use std::ops::{Add, FnOnce};
struct WhateverFn<F, T>(F, PhantomData<T>);
impl<F, T> FnOnce<(T,)> for WhateverFn<F, T> where F: FnOnce<(T,)> {
type Output = F::Output;
@rust-play
rust-play / playground.rs
Created August 29, 2018 23:51
Code shared from the Rust Playground
use std::ops::Index;
struct Example<T: 'static>(&'static [T]);
impl<F, I, T> Index<F> for Example<T>
where
F: FnOnce(usize) -> I,
[T]: Index<I>
{
type Output = <[T] as Index<I>>::Output;
@varkor
varkor / traits-as-type-constructors.md
Created August 28, 2018 14:58
A categorical model of traits in Rust

Traits as type constructors

This is a demonstration that we may view traits as type constructors: that is, functions from Type to Type. This view is motivated from a categorical perspective of type theory, in which the morphisms of the category take the central role. Cf. subobjects, which are represented by morphisms, rather than objects in a different category.

I'm assuming that the category of types, Type, is bicartesian closed. The set of morphisms between any two objects includes the set of functions denoted by Fn, FnMut and FnOnce in Rust.

impl Trait is Always Existential

Recently, Rust 1.26 was released, and with it came stable access to a heavily desired feature: impl Trait. For those unfamiliar, impl Trait lets you write something like this:

fn foo<'a, A, B>(x: &'a mut A) -> impl Bar<'a, B> { ... }

and have it be translated to something like this:

@withoutboats
withoutboats / constraint-kinds.md
Last active January 9, 2023 17:14
Constraint Kinds in Rust

Notes on adding Constraint Kinds to Rust

Background: Constraint Kinds

a trait can be thought of as a type operator generating a "constraint" - what in Rust would usually be called a bound. For example:

// Declares a new item `Foo` with kind `type -> constraint`
trait Foo { }
@insaneinside
insaneinside / draft.markdown
Created September 13, 2017 01:22
Whole-Crate Test-Case Reduction with C-Reduce

Whole-Crate Test-Case Reduction with C-Reduce

Sometimes, when working on a complex piece of software — or even a simple program or library with a non-trivial number of moving parts — you run into an issue and have absolutely no idea how to reduce your code to the minimum necessary in order to reproduce the error.

For the past several months I've been blocked by some trait-resolution errors in Symtern, my general-purpose interner crate. I knew the errors were somehow related to

anonymous
anonymous / playground.rs
Created September 17, 2016 23:40
Shared via Rust Playground
trait ShortCircuit {
fn short(&self) ->
}
trait And<Rhs=Self> {
type Output;
fn short(&self) -> Option<Self::Output>;
fn and(self, rhs: Rhs) -> Self::Output;
}