Skip to content

Instantly share code, notes, and snippets.

View rpn3319's full-sized avatar

Raymond Phu Nguyen rpn3319

  • Ho Chi Minh City, Vietnam
  • 09:53 (UTC +07:00)
  • LinkedIn in/rpn19
View GitHub Profile
@rpn3319
rpn3319 / type-state.rs
Created June 29, 2024 10:43
Rust Type-State pattern
trait FileState {}
// We need this to define
// `close` method for multiple states
trait Closable {
fn close(self) -> File<ClosedFile>;
}
struct File<S: FileState> {
state: S,
@rpn3319
rpn3319 / lint-test.yaml
Last active June 29, 2024 02:20
Github Actions for Rust
name: Rust
on:
pull_request:
branches: [ "master" ]
env:
CARGO_TERM_COLOR: always
jobs:
@rpn3319
rpn3319 / graphql-upload.js
Created April 21, 2024 07:37
Simple usage of graphql-upload
const { createReadStream } = require('fs');
const { promisify } = require('util');
const stream = require('stream');
const { GraphQLUpload } = require('graphql-upload');
// Create a promisified version of the `pipeline` function
const pipeline = promisify(stream.pipeline);
const resolvers = {
Mutation: {
@rpn3319
rpn3319 / directive.js
Last active April 19, 2024 09:59
Snippet GraphQL directive with @graphql-tools/utils
import gql from 'graphql-tag';
import { MapperKind, mapSchema, getDirectives } from '@graphql-tools/utils';
const typeDef = gql`
directive @ownerType(t: String! ) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
`;
const directiveName = typeDef.definitions[0].name.value;
const transformer = (schema) => {
return mapSchema(schema, {
[MapperKind.OBJECT_FIELD]: (fieldConfig, fieldName, parentTypeName) => {
const directive = getDirectives(schema, fieldConfig).find(
@rpn3319
rpn3319 / postgres-backup-restore.sh
Created October 8, 2023 13:35
postgres backup restore commands
pg_dump -Ft dbname > backup.tar
pg_restore -Ft backup.tar | psql dbname
# or this for much better compression
pg_dump -Ft dbname | gzip > backup.tar.gz
gunzip -c backup.tar.gz | pg_restore -Ft -d dbname
# with custom host and username
pg_dump -h localhost -p 5433 -U postgres -Ft dbname .....
@rpn3319
rpn3319 / rust-pointers-examine.rs
Last active October 8, 2023 01:57
rust pointers examine
use std::mem::size_of;
static B: [u8; 10] = [99, 97, 114, 114, 121, 116, 111, 119, 101, 108];
static C: [u8; 11] = [116, 104, 97, 110, 107, 115, 102, 105, 115, 104, 0];
fn main() {
let a: usize = 42;
let b: &[u8; 10] = &B;
let c: Box<[u8]> = Box::new(C);
fn main() {
let a: f32 = 42.42;
let b: f32 = -42.42;
let a_bits: u32 = a.to_bits();
let b_bits: u32 = b.to_bits();
let a_sign: u32 = a_bits >> 31;
let b_sign: u32 = b_bits >> 31;
println!("a_sign: {}", a_sign); // a_sign: 0
use std::mem::transmute;
fn main() {
let big_endian: [u8; 4] = [0xAA, 0xBB, 0xCC, 0xDD];
let little_endian: [u8; 4] = [0xDD, 0xCC, 0xBB, 0xAA];
let a: i32 = unsafe { transmute(big_endian) };
let b: i32 = unsafe { transmute(little_endian) };
println!("{} vs {}", a, b);
fn main() {
let a: f32 = 42.42;
let frankentype: u32 = unsafe {
println!("{}", a);
std::mem::transmute(a)
};
println!("{}", frankentype);
println!("{:032b}", frankentype);
use thiserror::Error
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum PuzzleError {
#[error("Piece {0} doesn't fit!")]
WontFit(u16),
#[error("Missing a piece")]
MissingPiece,
}