Skip to content

Instantly share code, notes, and snippets.

Petr Zemek s3rvac

View GitHub Profile
s3rvac /
Created May 3, 2018
Limits the maximal virtual memory for a subprocess in Python on Linux.
#!/usr/bin/env python3
# Limits the maximal virtual memory for a subprocess in Python.
# Linux only.
import subprocess
import resource
s3rvac / limit-virtual-memory.c
Created Dec 16, 2017
Limiting the maximal virtual memory of a process within itself on Linux.
View limit-virtual-memory.c
// Limits the maximal virtual memory of the process to half of the total
// amount of RAM on the system.
// Linux only.
// Compilation:
// gcc -std=c11 -pedantic limit-virtual-memory.c -o limit-virtual-memory
s3rvac / visit-variant.cpp
Last active Jul 11, 2018
Visiting std::variant using lambda expressions in C++17
View visit-variant.cpp
// $ g++ -std=c++17 -pedantic -Wall -Wextra visit-variant.cpp -o visit-variant
// $ ./visit-variant
// Implementation:
// Based on
#include <variant>
template<typename... Ts> struct make_overload: Ts... { using Ts::operator()...; };
s3rvac /
Created Apr 17, 2017
Converting a Vec<Item> to Vec<Option<Item>> in Rust
// We need to use Vec::into_iter() instead of Vec::iter() to create a consuming
// iterator that moves each value out of the vector.
fn main() {
let v = vec![1, 2, 3];
let v: Vec<Option<i32>> = v.into_iter().map(Some).collect();
assert_eq!(v, [Some(1), Some(2), Some(3)]);
s3rvac /
Created Apr 16, 2017
A Rust function that accepts both String or &str and creates a copy of it
// Item::new() accepts both &str and String. When called, it creates a copy of
// the parameter and uses it to initialize the returned item.
// The trick is in the use of the Into trait:
struct Item {
id: String
s3rvac / cpp11-string-literal.cpp
Created Mar 29, 2015
Emulation of the standard std::string literal from C++14 in C++11.
View cpp11-string-literal.cpp
// $ g++ -std=c++11 -pedantic -Wall -Wextra cpp11-string-literal.cpp -o cpp11-string-literal
// $ ./cpp11-string-literal
#include <string>
// Emulates the standard std::string literal ("..."s) from C++14. Since 's' is
// reserved by the standard, we have to use '_s' instead of 's'.
std::string operator "" _s(const char *str, size_t length) {
return std::string(str, length);
s3rvac / double-dispatch.cpp
Last active Aug 29, 2015
An example of using double dispatch in C++ to implement expression evaluation without type casts.
View double-dispatch.cpp
// $ g++ -std=c++14 -pedantic -Wall -Wextra double-dispatch.cpp -o double-dispatch
// $ ./double-dispatch
// Also works under C++11.
#include <iostream>
#include <memory>
#if __cplusplus == 201103L
namespace std {
You can’t perform that action at this time.