Skip to content

Instantly share code, notes, and snippets.

@mexus
mexus / parse.py
Created July 7, 2023 21:38
Simple regex bench
#!/usr/bin/env python3
import re
import random
import string
import time
import timeit
from typing import List
regex = re.compile(r"<(?P<name>[^>]+)>")
@mexus
mexus / tim2.rs
Last active December 7, 2020 22:45
trying to set up a timer
static TIMER_COUNTER: AtomicUsize = AtomicUsize::new(0);
#[interrupt]
fn TIM2() {
TIMER_COUNTER.fetch_add(1, Ordering::Relaxed);
}
#[cortex_m_rt::entry]
fn main() -> ! {
use std::{
f64::consts::PI,
io::Write,
time::{Duration, Instant},
};
use humantime::parse_duration;
use spidev::{SpiModeFlags, Spidev, SpidevOptions};
use structopt::StructOpt;
@mexus
mexus / arc_benchmark.rs
Created July 15, 2020 16:57
`Arc::clone` performance
use criterion::{criterion_group, criterion_main, Criterion};
use std::sync::Arc;
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("arc", |b| {
let arc = Arc::new(String::from("Some string"));
b.iter(|| {
let clone = Arc::clone(&arc);
clone
})
@mexus
mexus / concat.rs
Last active July 5, 2020 13:12
benchmark of various concatenation methods of a small and a large strings
// In `Cargo.toml`:
// [dev-dependencies]
// criterion = "0.3.3"
//
// [[bench]]
// name = "concat"
// harness = false
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
@mexus
mexus / lib.rs
Created May 3, 2020 20:31
Solution to "Data Stream as Disjoint Intervals" Leetcode problem
use std::{
cmp::{self, Ordering},
collections::BTreeSet,
};
#[derive(Default)]
pub struct SummaryRanges {
ranges: BTreeSet<ClosedRange>,
}
@mexus
mexus / file_to_part.rs
Created September 7, 2019 10:56
Converts a file into reqwest::async::multipart::Part
use bytes::{Bytes, BytesMut};
use reqwest::r#async::multipart::Part;
use std::{io, path::Path};
use tokio::{
codec::{Decoder, FramedRead},
prelude::*,
};
struct FileCodec;
impl Decoder for FileCodec {
@mexus
mexus / main.rs
Last active July 11, 2019 13:08
enum variants as keys in a toml dictionary
// [dependencies]
// serde = { version = "1", features = [ "derive" ] }
// strum = "0.15.0"
// strum_macros = "0.15.0"
// toml = "0.5.1"
use serde::{
de::{self, Error as _},
Deserialize, Deserializer,
};
@mexus
mexus / faq.md
Last active September 15, 2018 22:31 — forked from polachok/faq.md
Futures & tokio FAQ (ru)

Что такое future?

Future это тип, для которого имплементирован трейт Future

Кто вызывает poll()?

Executor. В общем случае, это может быть ивент-луп ( цикл событий,

diff --git a/src/main.rs b/src/main.rs
index b43471a..9a015a6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -127,7 +127,7 @@ fn init_ws(r: &Client, &conn: &Connection, url: &str) {
sink.send(Message::Text(subscribe.to_string()))
.map_err(FHError::Send)
- .and_then(|_| {
+ .and_then(|sink| {