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 / 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 / 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 / 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,
@jpastuszek
jpastuszek / playground.rs
Created May 16, 2019 11:01 — forked from rust-play/playground.rs
Code shared from the Rust Playground
use std::marker::PhantomData;
use std::convert::TryFrom;
use std::convert::TryInto;
#[derive(Debug)]
struct ConversionError;
struct ResultSet<T> {
column_names: Vec<String>,
rows: Vec<Vec<i32>>,
@jpastuszek
jpastuszek / playground.rs
Created May 16, 2019 10:34 — forked from rust-play/playground.rs
Code shared from the Rust Playground
use std::rc::Rc;
struct ResultSet {
column_names: Rc<Vec<String>>,
rows: Vec<Vec<i32>>,
}
impl ResultSet {
fn pop(&mut self) -> Option<Row> {
self.rows.pop().map(|columns| Row {
@jpastuszek
jpastuszek / playground.rs
Created May 16, 2019 10:29 — forked from rust-play/playground.rs
Code shared from the Rust Playground
struct ResultSet {
column_names: Vec<String>,
rows: Vec<Vec<i32>>,
}
impl ResultSet {
fn get<'rs>(&'rs self, row_no: usize) -> Option<Row<'rs>> {
self.rows.get(row_no).map(|columns| Row {
column_names: self.column_names.as_slice(),
columns: columns.as_slice(),