Skip to content

Instantly share code, notes, and snippets.

@U007D
U007D / main.rs
Created Mar 26, 2022 — forked from jix/main.rs
Lifetime GAT emulation on stable rust
View main.rs
// This is a technique to emulate lifetime GATs (generic associated types) on stable rust starting
// with rustc 1.33.
//
// I haven't seen this exact technique before, but I would be surprised if no one else came up with
// it. I think this avoids most downsides of other lifetime GAT workarounds I've seen.
//
// In particular, neither implementing nor using traits with emulated lifetime GATs requires adding
// any helper items. Only defining the trait requires a single helper trait (+ a single helper impl
// for the 2nd variant) per GAT. This also makes the technique viable without any boilerplate
// reducing macros.
@U007D
U007D / openocd-unmatched.cfg
Created Jul 17, 2021 — forked from a4lg/openocd-unmatched.cfg
HiFive Unmatched + OpenOCD configuration (S7+U7 cores; no flash configured)
View openocd-unmatched.cfg
adapter speed 10000
adapter driver ftdi
ftdi_device_desc "Dual RS232-HS"
ftdi_vid_pid 0x0403 0x6010
ftdi_layout_init 0x0008 0x001b
ftdi_layout_signal nSRST -oe 0x0020 -data 0x0020
set _CHIPNAME riscv
transport select jtag
@U007D
U007D / dedup.rs
Created Sep 4, 2020
imperative vs declarative loop - a case where the traditional for loop is easier to read AND write
View dedup.rs
fn iterative_save<W: Write>(&self, mut wtr: W) -> Result<&Self> {
for row in self {
for (i, col) in row.iter().enumerate() {
match i {
0 => wtr.write_all(col.as_bytes())?,
_ => {
wtr.write_all(b",")?;
wtr.write_all(col.as_bytes())?
}
}
@U007D
U007D / cg_bowling_game.rs
Last active Jan 4, 2020
Code Golf Bowling Game
View cg_bowling_game.rs
fn main() {
let game = "X 7/ 9- X F8 ⑧/ F6 X X X8/"
.chars()
.filter(|c| !c.is_whitespace())
.fold(Vec::new(), |mut rolls, roll| {
match roll {
'X' => rolls.push(10_u8),
'/' => rolls.push(10 - rolls.last().expect("invalid game score")),
c if c >= '0' && c <= '9' => rolls.push((u32::from(c) - 0x30) as u8),
c if c >= '⑤' && c <= '⑧' => rolls.push((u32::from(c) - 0x245f) as u8),
View AutoFacGettingStarted.rs
#![feature(existential_type)]
use chrono::Local;
use std::io::{self, Write};
type Result<T> = std::result::Result<T, failure::Error>;
trait IOutput {
fn write(&self, content: &String) -> Result<()>;
}
struct ConsoleOutput {}
View AutoFacGettingStarted.cs
using System;
using Autofac;
namespace DemoApp
{
// This interface helps decouple the concept of
// "writing output" from the Console class. We
// don't really "care" how the Write operation
// happens, just that we can write.
public interface IOutput
@U007D
U007D / di.rs
Last active Apr 18, 2019
Simple Dependency Injection
View di.rs
#![feature(existential_type)]
type Result<T> = std::result::Result<T, failure::Error>;
trait MyTrait {
fn greet(&self) -> String;
}
struct MyType {}
View .bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
@U007D
U007D / default_clippy_lints.rs
Created Oct 6, 2018
Strict + Safety-Critical `clippy` default lint specification - current as per `clippy` v0.0.212 (2018-10-03)
View default_clippy_lints.rs
#![forbid(overflowing_literals,)]
#![deny(unsafe_code)] // Do not remove! Change to `allow` to explicitly opt-in to using `unsafe` (facilitates auditing)
// vvv Safety-critical application lints (pedantic: use for safety-critical applications only) vvv
#![deny(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_precision_loss,
clippy::cast_sign_loss, clippy::float_cmp_const, clippy::indexing_slicing, clippy::integer_arithmetic,
clippy::maybe_infinite_iter, clippy::option_unwrap_used, clippy::result_unwrap_used,)]
// ^^^ End of safety-critical lint section ^^^
#![warn(clippy::clone_on_ref_ptr, clippy::decimal_literal_representation, clippy::default_trait_access,
clippy::doc_markdown, clippy::else_if_without_else, clippy::empty_enum, clippy::enum_glob_use,
clippy::expl_impl_clone_on_copy, clippy::fallible_impl_from, clippy::filter_map, clippy::if_not_else,