Skip to content

Instantly share code, notes, and snippets.

View aboglioli's full-sized avatar
💡

Alan Boglioli aboglioli

💡
View GitHub Profile
@aboglioli
aboglioli / closure_as_struct_property.rs
Created May 9, 2020 20:34
Closures as struct property: using generics and using trait object.
struct Data<F>
where
F: FnMut(&i32) -> i32,
{
value: i32,
dyn_value: i32,
// parser, once assigned, cannot change because each closure is unique
parser: F,
dyn_parser: Box<dyn FnMut(&i32) -> i32>, // dynamic, parser can change any time
}
@aboglioli
aboglioli / trait_object_in_generic_argument.rs
Last active May 9, 2020 18:30
Trait object passed as generic argument by using ?Sized as relaxation.
use std::fmt;
trait Handler: fmt::Debug {
fn handle(&mut self);
}
#[derive(Debug)]
struct Value(i32);
#[derive(Debug)]
struct Data {
@aboglioli
aboglioli / iterator_on_trait_objects.rs
Last active May 9, 2020 06:11
Iterator<Item = &'a mut T> on trait objects.
use std::fmt;
use std::slice::IterMut;
/**
* Data
*/
trait Data: fmt::Debug {
fn add_value(&mut self, v: i32);
}
@aboglioli
aboglioli / share_property_with_child.rs
Created May 8, 2020 04:48
Share struct property (owned) with childs through reference.
use core::fmt::Debug;
use std::rc::Rc;
#[derive(Debug)]
struct Display;
impl Display {
fn print<T: Debug>(&self, t: &T) {
println!("> {:?}", t);
}
@aboglioli
aboglioli / dynamic_dispatch.rs
Created May 8, 2020 03:53
Dynamic dispatch with Box and trait objects. Function handler.
trait Handler {
fn handle(&self);
}
struct Data1 {
v: i32,
}
impl Handler for Data1 {
fn handle(&self) {
println!("Data1: {}", self.v);
@aboglioli
aboglioli / struct_self_reference.rs
Last active May 3, 2020 23:24
Example of self-referenced struct. World struct has multiple players and a property called 'current' referencing one of the players in vector.
use std::cell::RefCell;
use std::rc::Rc;
// Using Rc and RefCell
#[derive(Debug)]
struct Player {
name: String,
}
#[derive(Debug)]
@aboglioli
aboglioli / iterator_as_arg.rs
Last active May 3, 2020 07:50
Iterator as function argument, implemented with genercis and trait object.
use std::collections::HashMap;
use std::iter::Iterator;
// Data
#[derive(Debug)]
struct Common(i32);
#[derive(Debug)]
struct Data<'a> {
id: i32,
ALTER TABLE `dimHora` CHANGE `id_hora` `id_hora` VARCHAR(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL;
CREATE INDEX princ ON dimBase(id_base);
CREATE INDEX princ ON dimCodigo(id_codigo);
CREATE INDEX princ ON dimDesenlace(id_desenlace);
CREATE INDEX princ ON dimHora(id_hora);
CREATE INDEX princ ON dimInstitucion(id_institucion);
CREATE INDEX princ ON dimMovil(id_movil);
CREATE INDEX princ ON dimTiempo(id_fecha);
CREATE INDEX princ ON dimZona(id_zona);
@aboglioli
aboglioli / func_decl_test.go
Last active December 17, 2019 22:31
Go: function declaration vs var (benchmark)
package main
import "testing"
func sum1(a, b int) int {
return a + b
}
func BenchmarkDefaultDeclaration(b *testing.B) {
for i := 0; i < b.N; i++ {
@aboglioli
aboglioli / PhpArrayToJSON.php
Created February 16, 2017 23:58
PHP Array to JSON
<?php
$datos = [
"title"=> "Property title",
"category_id"=> "MLU1468",
"price"=> 100000,
"currency_id"=> "UYU",
"available_quantity"=> 1,
"buying_mode"=> "classified",
"listing_type_id"=> "silver",
"condition"=> "not_specified",