Skip to content

Instantly share code, notes, and snippets.

View Havvy's full-sized avatar

Ryan Scheel Havvy

  • Work is not programming related
  • Washington, USA
View GitHub Profile
<Havvy> Thought: An attribute for enums that says there are no invalid values for it, but requires `_ =>` in the match always. And then transmuting is legal / using in FFI has less chance of triggering undefined behaviour.
* jonhoo has quit (Quit: Leaving)
<Soni> non-exhaustive enums?
* AstralSorcerer has quit (Ping timeout: 121 seconds)
* aismallard ([email protected]) has joined
* AstralSorcerer ([email protected]) has joined
<est31> enums where at definition site you can say
<est31> "this is not the complete list, every match needs to contain a _ => case"
<sfackler> Havvy: there was talk in the non exhaustive enum PR to make #[non_exhaustive] #[repr(C)] behave like that but it didn't make it in. i'd love to have something like that for FFI though
<Havvy> sfackler: Representation hints cannot (should not?) change the validity of `match`, but #[non_exhaustive] #[repr(C)] would do that. So it'd have to be a different attribute.
[havvy@rustc:~/workspace/rust/project]$ ./x.py test src/libstd --stage=1
Updating submodules
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package: /home/havvy/workspace/rust/project/src/tools/cargo/Cargo.toml
workspace: /home/havvy/workspace/rust/project/src/Cargo.toml
Finished dev [unoptimized] target(s) in 0.0 secs
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
package: /home/havvy/workspace/rust/project/src/tools/cargo/Cargo.toml
workspace: /home/havvy/workspace/rust/project/src/Cargo.toml
warning: profiles for the non root package will be ignored, specify profiles at the workspace root:
@Havvy
Havvy / Rustbot.md
Last active October 15, 2017 04:44

Rustbot is the helpful IRC bot found in many of the Rust IRC channels on irc.mozilla.org. Rustbot is maintained by Havvy and is a Tennu bot.

Using Rustbot

Rustbot only responds to intentional commands. You can say one of the following to trigger it:

  • !command args
  • rustbot: !command args
  • arbitrary text {!command args} arbitrary text
  • Private messaging rustbot is supported.
┌─         ─┐
│ 1 0 0 0 ⋯│
│ 0 1 0 0 ⋯│
│ 0 0 1 0 ⋯│
│ 0 1 0 1 ⋯│
│ 0 0 0 0 ⋯│
│ 0 0 1 1 ⋯│
│ 0 0 1 1 ⋯│
│ 0 0 0 1 ⋯│

│ 0 0 0 1 ⋯│

[email protected] /home/havvy/workspace/tennu/tennu
├── @havvy/[email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]

Programmers have a tendency to write functions that take types that are too generic for what the functions actually want.

The most common form of this is called Boolean Blindness where a function takes boolean arguments that don't actually say what they want. An example that comes to mind is widget.paint(true);. It's impossible to say what that boolean parameter does without looking up the documentation. The general solution is to either take an enum (short for enumeration) or an approximation thereof. It'd look something like widget.paint(WidgetPaintMode.Immediate);.

In generic this is called Primitive Obsession. Functions that take integers, booleans, strings, and other highly general types. And the solution is to create a more specialized type, either by wrapping the type in a wrapper or creating a new type type to represent the concept, ideally making it impossible to represent impossible values.

Programmers also have an error handling strategy where they return a type that represents *either

// I'm not sure how much the Copy bound is actually necessary...
trait LeftRight<T: Copy> {
left: T,
right: T
}
impl<C: Copy, LR: LeftRight<C>, T: LR> T {
fn swap(&mut self) {
let temp = self.right;
@Havvy
Havvy / mibbit.db.json
Last active June 30, 2016 07:44
Factoids for Havvy's bots.
{"key":"u"}
{"key":"HM 3"}
{"key":"havvy","val":"false profit"}
{"key":"havvy","val":"false profit"}
{"key":"Loser","val":"fag"}
{"key":"HM02","val":""}
{"key":"Lord_Diamond","val":"God"}
{"key":"Ryan","val":"Sollux IRL"}
{"key":"Moltres","val":"Volleyball"}
{"key":"vein","val":"ebola"}

Import with args

Summary

A method by which the programmer can import a module multiple times, each time differing on a dynamic value while still delivering on the static property guarantee.

Example

@Havvy
Havvy / FizzBuzz.rs
Created June 15, 2015 23:51
I went with using an Iterator instead of the traditional if/else chain.
use std::fmt;
enum FizzBuzz {
Fizz,
Buzz,
}
impl fmt::Display for FizzBuzz {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {