Skip to content

Instantly share code, notes, and snippets.

@AnthonyMikh
AnthonyMikh / main.rs
Created September 27, 2021 14:50
Решение задачи №285 от UniLecs
fn split_consecutive_identical_str(s: &str) -> impl Iterator<Item = (u8, usize)> + '_ {
split_consecutive_identical(s.as_bytes())
}
fn split_consecutive_identical(mut seq: &[u8]) -> impl Iterator<Item = (u8, usize)> + '_ {
std::iter::from_fn(move || {
let (&first, rest) = seq.split_first()?;
let cut_off = rest
.iter()
.position(|&ch| ch != first)
@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 / main.rs
Created September 2, 2021 21:11
Extracting arbitrary field sets from JSON
use serde::Deserialize;
struct HNil;
impl<'de> Deserialize<'de> for HNil {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_any(serde::de::IgnoredAny)?;
@AnthonyMikh
AnthonyMikh / main.rs
Created August 8, 2021 23:44
Инициализация массивов a la C++
trait ConstZero {
const ZERO: Self;
}
macro_rules! impl_primitive {
($($ty:ty)*) => {
$(
impl ConstZero for $ty {
const ZERO: Self = 0;
}
@AnthonyMikh
AnthonyMikh / main.rs
Created August 5, 2021 23:06
Automatic cleanup of resources wuth a specified operation
struct AutoDo<T, F> {
value: T,
action: F,
}
struct DoOnDrop<'a, T, F: FnMut(&mut T)>(&'a mut AutoDo<T, F>);
impl<T, F> AutoDo<T, F> {
fn new(value: T, action: F) -> Self {
Self { value, action }
@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
Created February 5, 2021 16:58
Macro for creating auto-enumerated constants in Golang's iota-like way
macro_rules! iota_consts {
(
$iota:ident: $($tt:tt)*
) => {
iota_consts!(
$iota(0, $iota): $($tt)*
);
};
(
@AnthonyMikh
AnthonyMikh / main.rs
Last active December 5, 2020 21:55
Разбор ASCII-строк с игнорированием регистра
#[allow(dead_code)]
const fn is_ascii_lowercase(s: &str) -> bool {
let s = s.as_bytes();
let len = s.len();
let mut i = 0;
while i < len {
if !s[i].is_ascii() || s[i].is_ascii_uppercase() {
return false;
}
i += 1;
@AnthonyMikh
AnthonyMikh / main.rs
Created November 27, 2020 19:35
Реализация макросов для разбора строк по префиксам с детекцией повторяющихся паттернов
macro_rules! prefixes {
(match $value:ident {
$($prefix:literal .. $(if $condition:expr)? => $arm:expr,)*
_ => $catch_all:expr $(,)?
}) => {{
#[allow(dead_code)]
fn non_repeating() {
#[warn(unreachable_patterns)]
match "" {
$($prefix => (),)*
@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)]