Skip to content

Instantly share code, notes, and snippets.

View Ciantic's full-sized avatar

Jari Pennanen Ciantic

View GitHub Profile
@Ciantic
Ciantic / tuple_layout.rs
Created April 14, 2021 19:27
Tuple layout
#[repr(C)]
struct Foo(f32, f32, f32);
fn main() {
let a = [Foo(1.0,2.0,3.0), Foo(4.0,5.0,6.0)];
let b = [1.0f32,2.0,3.0,4.0,5.0,6.0];
let c = [(1.0f32,2.0f32,3.0f32), (4.0f32,5.0f32,6.0f32)];
let amem = unsafe { std::slice::from_raw_parts((&a as *const _) as *const u8, std::mem::size_of_val(&a) )};
let bmem = unsafe { std::slice::from_raw_parts((&b as *const _) as *const u8, std::mem::size_of_val(&b) )};
let cmem = unsafe { std::slice::from_raw_parts((&c as *const _) as *const u8, std::mem::size_of_val(&c) )};
@Ciantic
Ciantic / build.rs
Created February 25, 2021 17:50
Some in-memory stuff I threw away
use std::io::Write;
use std::{env, path::Path};
use std::{fs::OpenOptions, path::PathBuf};
use regex::Regex;
fn main() {
let dir = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap()).join("lib");
println!("cargo:rustc-link-search=native={}", dir.display());
@Ciantic
Ciantic / druid_d2d.rs
Created February 19, 2021 23:16
set_blend_mode, get_blend_mode
//! Convenience wrappers for Direct2D objects.
//!
//! These also function as safety boundaries (though determining the
//! exact safety guarantees is work in progress).
// TODO: get rid of this when we actually do use everything
#![allow(unused)]
use std::ffi::c_void;
use std::fmt::{Debug, Display, Formatter};
@Ciantic
Ciantic / YAML-to-JSON-with-Rust.rs
Created February 3, 2021 16:39
YAML to JSON with Rust
// Yaml to JSON with Rust
// Dependencies serde_yaml and serde_json
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3ddd4d9a65f03b717eeb1258299e9503
fn main() {
let json_value: serde_json::Value = serde_yaml::from_str(&"
---
test: some value here
another: value here
third:
@Ciantic
Ciantic / parse-url-search-params-to-object.js
Last active September 30, 2020 19:35
Parse url search parameters to object
Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => ({ ...a, [k]: [...a[k] ?? [], v] }), {});
// {foo: ["1","2","3"], blah: ["a","b"]}
// Or directly to string
Array.from(new URLSearchParams("foo=1&foo=2&foo=3&blah=a&blah=b").entries()).reduce((a,[k,v]) => a.has(k) && (a.set(k, a.get(k) + "," + v) ?? a) || (a.set(k, v) ?? a), new URLSearchParams()).toString();
// "foo=1%2C2%2C3&blah=a%2Cb"
@Ciantic
Ciantic / sql-safe-field-names-with-typescript.ts
Last active September 15, 2020 18:03
Safeish SQL field names with typescript
function fields<T>() {
return new Proxy(
{},
{
get: function (_target, prop, _receiver) {
return prop;
},
}
) as {
[P in keyof T]: P;
use com::{
runtime::{init_apartment, ApartmentType},
sys::CoCreateInstance,
ComInterface, ComPtr, ComRc, CLSID, IID,
};
fn errorhandler<T, F>(f: F, error: HRESULT, retry: u32) -> Result<T, HRESULT>
where
F: Fn() -> Result<T, HRESULT>,
{
@Ciantic
Ciantic / postcsstest.js
Created June 20, 2020 21:47
Use postcss from deno
import autoprefixer from "https://jspm.dev/autoprefixer";
import postcss from "https://jspm.dev/postcss";
postcss().use(autoprefixer).process(".something { appearance: none; }", {
from: undefined
}).then(f => {
console.log(f.css);
})
@Ciantic
Ciantic / wp-allow-svg.php
Last active February 18, 2023 05:19
WordPress allow uploading SVG, even without the XML declaration
<?php
// Mind you, this does not make SVG files safe. This script is meant for sites where only trusted people can upload.
add_action("init", function() {
// First line of defence defused
add_filter('upload_mimes', function ($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
});
@Ciantic
Ciantic / preact-custom-element.ts
Last active June 14, 2020 20:41
This is preact custom element example (incomplete)
import { h, render } from 'preact';
import type { FunctionComponent } from 'preact';
// This makes it recgonized by the JSX parts:
declare global {
namespace preact.createElement.JSX {
interface IntrinsicElements {
'oksidi-sharer': { 'share-url': string; };
}