Skip to content

Instantly share code, notes, and snippets.

@jpastuszek
jpastuszek / pin.rs
Last active April 25, 2023 13:52 — forked from rust-play/playground.rs
Pin and PhantomPinned
use core::pin::Pin;
use core::marker::PhantomPinned;
#[derive(Debug)]
struct Unm(i32, PhantomPinned); // !Unpin
//struct Unm(i32); // Unpin
fn foo() -> Pin<Box<Unm>> {
// PhantomPinned is !Unpin so Pin does not implement get_mut (disallowing mem::swap/replece and then move) nor into_inner (move out Unm of Box)
Box::pin(Unm(1, PhantomPinned))
@jpastuszek
jpastuszek / playground.rs
Last active December 20, 2022 15:04 — forked from rust-play/playground.rs
Return impl Trait generic trainsient borrow
#![feature(type_alias_impl_trait)]
use std::io::Read;
use std::io::Cursor;
struct B(String);
struct RefPairA<'b>(&'b B, String);
trait GetPair {
fn pair(&self) -> (&str, &str);
@jpastuszek
jpastuszek / smuggle.rs
Created March 5, 2021 17:24
Toy to export HTTP request smuggling
#!/usr/bin/env denim
/* Cargo.toml
[package]
name = "smuggle"
version = "0.1.0"
authors = ["Anonymous"]
edition = "2018"
[dependencies]
@jpastuszek
jpastuszek / playground.rs
Last active February 8, 2021 15:09 — forked from rust-play/playground.rs
Using vtable dispatch to work with owned type behind Box<dyn> trait
use std::any::{Any, TypeId};
use std::fmt::Debug;
pub trait Resource: Any + Debug {
// this returns correct function for the Self to work on Box so that it can be casted and unboxed
fn combine_fn(&self) -> fn(left: Box<dyn Resource>, right: Box<dyn Resource>) -> Box<dyn Resource>;
}
#[derive(Debug)]
struct File(u32);
@jpastuszek
jpastuszek / sync_cache.rs
Last active May 12, 2022 02:30 — forked from rust-play/playground.rs
Rust: RwLock Cache
use std::sync::Mutex;
use std::sync::RwLock;
use std::sync::Arc;
use std::collections::HashMap;
use lazy_static::lazy_static;
lazy_static! {
static ref CACHE: Mutex<HashMap<&'static str, Arc<RwLock<Option<u32>>>>> = Mutex::new(HashMap::new());
}
@jpastuszek
jpastuszek / raii_drop_phantop_ref.rs
Last active May 7, 2020 12:22 — forked from rust-play/playground.rs
Drop and pahntom ref unsound drop order
use std::marker::PhantomData;
struct Connection(u32);
impl Connection {
fn statement(&self) -> Statement {
Statement(Raii(self.0), PhantomData)
}
}
@jpastuszek
jpastuszek / mullvard.rs
Created January 31, 2020 17:31
Check mullvard.net IP status with Denim
#!/usr/bin/env denim
//
// Example script description
//
/* Cargo.toml
[package]
name = "mullvard"
version = "0.1.0"
@jpastuszek
jpastuszek / csv_const_generics.rs
Last active January 7, 2020 12:02 — forked from rust-play/playground.rs
Rust: Using const generics on trait to write CSV records of const number of columns
#![feature(const_generics)]
#![feature(const_generic_impls_guard)]
trait CsvRecord<const COLS: usize> {
fn columns() -> [&'static str; COLS];
fn values(&self) -> [String; COLS];
}
struct Foo {
id: u32,
@jpastuszek
jpastuszek / tracer.rs
Last active November 6, 2019 13:13
Groups GDB stack traces
#!/usr/bin/env denim
/* Cargo.toml
[package]
name = "tracer"
version = "0.1.0"
authors = ["Anonymous"]
edition = "2018"
[dependencies]
@jpastuszek
jpastuszek / playground.rs
Created July 8, 2019 09:41 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// https://stackoverflow.com/questions/50519147/double-mutable-borrow-error-in-a-loop-happens-even-with-nll-on
// https://github.com/rust-lang/rust/issues/54663
// This will complie with polonius: rustc +nightly -Zpolonius --edition=2018 /tmp/p.rs
struct Foo(u8);
impl Foo {
fn even<'i>(&'i mut self) -> &'i u8 {
loop {
match self.next() {
Some(even) => return even,