Skip to content

Instantly share code, notes, and snippets.

@AnthonyMikh
AnthonyMikh / Cargo.toml
Last active August 29, 2023 21:25
Как написать Fizzbuzz в .rodata
[package]
name = "fizzbuzz"
version = "0.1.0"
edition = "2021"
[features]
use_nightly = []
compare_with_previous_impl = []
enum Expr {
Lit(i32),
Neg(Box<Self>),
Add(Box<Self>, Box<Self>),
}
fn lit(x: i32) -> Expr {
Expr::Lit(x)
}
@AnthonyMikh
AnthonyMikh / lib.rs
Created April 9, 2022 00:42
Sorting by borrowed key (advanced variant requires GATs)
#![feature(generic_associated_types)]
pub struct Data {
pub name: String,
pub whatever: u32,
pub bytes: Vec<u8>,
}
trait BorrowedKeyFamily {
type Key<'a>: Ord + 'a;
@AnthonyMikh
AnthonyMikh / main.rs
Last active December 16, 2022 14:57
Crude parsing
#[derive(Clone)]
struct Lexer<'a> {
s: &'a str,
}
impl<'a> Lexer<'a> {
fn of(s: &'a str) -> Self {
Self { s }
}
@AnthonyMikh
AnthonyMikh / main.rs
Last active September 23, 2022 20:19
Реализация макроса по переводу константных числовых значений в строковые константы
macro_rules! make_literal_maker {
($name:ident : $ty:ident) => {
macro_rules! $name {
($n:expr) => {{
const fn digits_len(mut n: $ty) -> ::core::primitive::usize {
if n == 0 {
return 1;
}
let mut n_digits = 0;
#[allow(unused_comparisons)]
@AnthonyMikh
AnthonyMikh / typed.cpp
Created August 6, 2022 23:21
Typed versions of C memory managing utilities
#include <cstddef>
#include <cstring>
#include <cstdlib>
// #define TYPED_TIGHT_MALLOC
namespace typed {
template <typename T>
T* memset_n(T *dest, int ch, std::size_t count) {
@AnthonyMikh
AnthonyMikh / main.rs
Created September 16, 2021 17:41
Сопровождающий код к статье "Как написать FizzBuzz на собеседовании" (habr.com/ru/post/578198)
struct Z;
struct S<T>(T);
trait Add<Rhs> {
type Sum;
}
type SumOf<N, M> = <N as Add<M>>::Sum;
impl<N> Add<N> for Z {
@AnthonyMikh
AnthonyMikh / lib.rs
Last active May 16, 2022 22:00
Neumann and Moore neighborhood calculation for rectangular grid
#[derive(Clone, Copy)]
pub struct Bounds {
rows: usize,
cols: usize,
}
type Pos = (usize, usize);
impl Bounds {
pub fn around_neumann(
@AnthonyMikh
AnthonyMikh / main.rs
Created January 13, 2022 22:11
Двоичное дерево со статически ограниченной максимальной глубиной вложенности
use std::cmp::Ordering;
use std::convert::Infallible as Never;
use std::fmt::{self, Debug};
struct Z;
struct S<T>(T);
#[derive(Clone)]
struct Node<T, Next> {
value: T,
@AnthonyMikh
AnthonyMikh / lib.rs
Last active November 16, 2021 20:54
Решение задачи №292 от UniLecs (Расшифровывать строку)
struct Lexer<'a> {
s: &'a str,
}
type Num = usize;
#[derive(Debug)]
enum Lexeme<'a> {
String(&'a str),
Num(Num),