Skip to content

Instantly share code, notes, and snippets.

View bew's full-sized avatar
😎
https://nohello.net

Benoit de Chezelles bew

😎
https://nohello.net
View GitHub Profile
@bew
bew / literal_expander_in_macros.cr
Last active May 25, 2018 20:50
ArrayLiteral expansion using macros only
# Based on https://github.com/crystal-lang/crystal/blob/26635f1052b99ab5b52a8b051020a25435dba751/src/compiler/crystal/semantic/literal_expander.cr
macro expand_literal_ArrayLiteral_empty(node)
{% puts "--- expansion for: #{node}" %}
Array({{ node.of }}).new
{% debug %}
end
@bew
bew / manual_class.cr
Created September 21, 2018 20:07
Manually create a class in Crystal, from existing memory
#macro data(name, &block)
#end
#
# #### Ultimately I want to do this:
# and be able to make a pre-allocated buffer of Person,
# and be able to get a reference to a single Person (but pre-allocated
# in that buffer)
#
#data Person do
# property name : String
@bew
bew / class pre allocation.cr
Created September 22, 2018 22:03
Class pre-allocation in Crystal
class Object
alias TypeIdType = Int32
end
#
# This is unsafe to store any class created through the ObjectPool, as as
# soon the ObjectPool is GC-ed, all pointers to classes inside that pool
# will have unknown behavior if used.
class ObjectPool(T)
@buffer : Pointer(UInt8)
@bew
bew / scapy_script.py
Created October 6, 2018 10:20
ARP sniffer & force reply a pre-defined MAC address to every ARP request - using Scapy
#!/usr/bin/env python
from scapy.all import ARP, Ether, sniff, sendp
def arp_do_stuff(pkt):
if ARP not in pkt:
return
arp_display(pkt)
// References:
//
// Wikipedia IPv4 / IPv6:
// - https://en.wikipedia.org/wiki/IPv4
// - https://en.wikipedia.org/wiki/IPv6_packet
//
// Working implem:
// - https://www.binarytides.com/packet-sniffer-code-in-c-using-linux-sockets-bsd-part-2/
#include <unistd.h> // close
@bew
bew / rust enum in crystal.cr
Last active April 29, 2019 13:10
Rust's multi-type enum implementation in Crystal
# simplified example from rust:
# (https://github.com/srwalter/dbus-serialize/blob/b87fd9f78d9/src/types.rs)
#
# pub enum BasicValue {
# Byte(u8),
# Boolean(bool),
# Double(f64),
# Int16(i16),
# }
#
@bew
bew / fail1 - pluggable system in crystal.md
Last active May 6, 2019 21:02
FAIL 1 - Pluggable system in crystal

Doesn't work:

 % cr in_mem_system_env.cr
Error in pluggable_system_env.cr:25: class variable '@@current_impl' of PluggableSystem::Env must be PluggableSystem::Env::Interface:Module, not Crystal::System::Env:Module

  class_getter current_impl : Interface.class = OS_BACKEND
                                                ^~~~~~~~~~

It works!!

% cr in_mem_system_env.cr
system env: get SPEC_VERBOSE
system env: get HOME
mem env: get HOME
mem env: set HOME → my super home!
mem env: get HOME
system env: get HOME
@bew
bew / lsp_crystal_slow.log
Created September 23, 2019 22:41
Annotated LSP logs from LanguageClient-neovim for crystal server (slow...)
#######
LanguageClient 0.1.147
#######
# (nvim rpc) 23:03:42 INFO writer-None src/rpcclient.rs:215 => None {"jsonrpc":"2.0","method":"eval","params":["[!!get(g:, 'LanguageClient_autoStart', 1), s:GetVar('LanguageClient_serverCommands', {}), get(g:, 'LanguageClient_selectionUI', v:null), get(g:, 'LanguageClient_trace', v:null), expand(get(g:, 'LanguageClient_settingsPath', '.vim/settings.json')), !!get(g:, 'LanguageClient_loadSettings', 1), get(g:, 'LanguageClient_rootMarkers', v:null), get(g:, 'LanguageClient_changeThrottle', v:null), get(g:, 'LanguageClient_waitOutputTimeout', v:null), !!get(g:, 'LanguageClient_diagnosticsEnable', 1), get(g:, 'LanguageClient_diagnosticsList', 'Quickfix'), get(g:, 'LanguageClient_diagnosticsDisplay', {}), get(g:, 'LanguageClient_windowLogMessageLevel', 'Warning'), get(g:, 'LanguageClient_hoverPreview', 'Auto'), get(g:, 'LanguageClient_completionPreferTextEdit', 0), has('nvim')]"],"id":3}
# (nvim rpc) 23:03:42 INFO reader-None src/rpcclient.rs:169 <= None {"id": 3, "jsonrpc":
@bew
bew / default--hello-crystal.nix
Last active December 16, 2019 22:10
Hello world using Nix to package a crystal program
with import <nixpkgs> {};
stdenv.mkDerivation {
name = "test-crystal-project";
# Define that the build will need the crystal binary (and all its deps)
buildInputs = [
crystal
];