Skip to content

Instantly share code, notes, and snippets.

View jmeggitt's full-sized avatar

Jasper Meggitt jmeggitt

View GitHub Profile
@jmeggitt
jmeggitt / idiomatic_rust_ffi.md
Last active August 13, 2023 21:08
Notes on Common C FFI Patterns in Idiomatic Rust

Idiomatic FFI in Rust

Pointer concepts

Due to niche optimization, Rust will guarentee that the following types will all have the same layout in memory. However, each option comes with different guarentees and should be selected accordingly.

Non-null

  • &T
  • &mut T
  • NonNull<T>
    • *mut T, but non-zero and covariant
  • Box<T>
  • Same aliasing rules as &amp;mut T
@jmeggitt
jmeggitt / replys_with_errors_field.ndjson
Created November 16, 2022 00:09
This file contains every traceroute measurement which included a hop reply with an `error` field from https://data-store.ripe.net/datasets/atlas-daily-dumps/2022-10-14/traceroute-2022-10-14T0400.bz2
This file has been truncated, but you can view the full file.
{"fw":5080,"mver":"2.6.2","lts":3411695,"endtime":1665720293,"dst_name":"hitlist-rr6.hitlist.sdstrowes.co.uk","ttr":5091.217282,"dst_addr":"2408:815f:e000:1:18:1:0:14","src_addr":"fe80::dc6a:8c0f:b28c:9470","proto":"UDP","af":6,"size":48,"paris_id":2,"result":[{"hop":1,"result":[{"error":"bind failed: Invalid argument"}]}],"msm_id":24304870,"prb_id":1004666,"timestamp":1665720293,"msm_name":"Traceroute","from":"","type":"traceroute","group_id":24304869}
{"fw":5080,"mver":"2.6.2","lts":1,"endtime":1665720353,"dst_name":"202.12.27.33","dst_addr":"202.12.27.33","src_addr":"185.81.99.40","proto":"UDP","af":4,"size":40,"paris_id":9,"result":[{"hop":1,"result":[{"error":"sendto failed: Operation not permitted"}]}],"msm_id":5006,"prb_id":1000318,"timestamp":1665720353,"msm_name":"Traceroute","from":"185.81.99.40","type":"traceroute"}
{"fw":5070,"mver":"2.6.1","lts":28,"endtime":1665721102,"dst_name":"topology6.dyndns.atlas.ripe.net","ttr":126.817471,"dst_addr":"2a0f:14c0::1","src_addr":"2803:9400:454a:2d5a:ea94:f6ff
import cProfile, pstats
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()
def end_profile(log_file):
pr.disable()
with open(log_file, "w") as profile_out:
profile_stats = pstats.Stats(pr, stream=profile_out).sort_stats(SortKey.CUMULATIVE)
profile_stats.print_stats()
@jmeggitt
jmeggitt / RustDataStructuresCheatSheet.md
Last active July 30, 2022 23:54
A cheat sheet of data structures provided by different crates in Rust for different

(Incomplete)

Crate Type Owned Sync Send Mutable Inner Mutability Description
[bytes] [Bytes] ❌* ✔️ ✔️ Provides zero-copy sharing of byte buffers. A group of [Bytes]/[BytesMut] can share ownership of the underlying buffer similar to an Arc.
[bytes] [BytesMut] ✔️* ✔️ ✔️ ✔️ Similar to [Bytes] in functionality, but for exclusive ownership of a portion of a buffer.
[smallvec] SmallVec<[T;N]> ✔️ ✔️ Functions the same as a regular Vec<T>, but stores the first N elements on the stack before allocating space on the heap.
[generic-array] [GenericArray] ✔️ :grey_quest
[INFO][amethyst::app] Initializing Amethyst...
[INFO][amethyst::app] Version: 0.13.2
[INFO][amethyst::app] Platform: x86_64-pc-windows-msvc
[INFO][amethyst::app] Amethyst git commit:
[INFO][amethyst::app] Rustc version: 1.39.0 Stable
[INFO][amethyst::app] Rustc git commit: 4560ea788cb760f0a34127156c78e2552949f734
[INFO][winit::platform::platform::window] Guessed window DPI factor: 1
[WARN][rendy_factory::factory] Slow safety checks are enabled! Disable them in production by enabling the 'no-slow-safety-checks' feature!
[INFO][rendy_util::wrap] Slow safety checks are enabled! You can disable them in production by enabling the 'no-slow-safety-checks' feature!
[INFO][gfx_backend_vulkan]
@jmeggitt
jmeggitt / world_storage.rs
Last active September 13, 2019 16:30
An idea I had for how storage could be managed in nphysics.
//! The idea of this system is to abstract the Set type, remove the need for `pop_insertion_event`
//! and `pop_removal_event`, and abstract the maintain functions of both worlds. The only issue I
//! have had so far is trying to figure out what WorldStorage should be responsible over compared to
//! GeometricalWorld.
//!
//! Goal:
//! - Give ecs control over how storages are maintained
//! - Make the implementation of Set consistent for bodies, colliders, and constraints
//! - Bundle all of the storages into one convenient struct (low priority)
#![allow(missing_docs)]
@jmeggitt
jmeggitt / system_data.rs
Created August 25, 2019 17:47
Make regular rigid bodies from nphysics2d/3d visible in specs
use specs::{WriteStorage, WriteExpect, ReadStorage, SystemData, Join, Resources, Component, BitSet};
use specs::storage::UnprotectedStorage;
use specs::Entity;
use specs::shred::ResourceId;
use specs::storage::{MaskedStorage, AnyStorage, FlaggedStorage, DenseVecStorage};
use specs::shred::MetaTable;
use specs::storage::TryDefault;
use nphysics::object::BodyHandle;
use nphysics::object::RigidBody;
Prefab(
entities: [
// Grey bat
PrefabEntity(
data: MyPrefabData(
// SpriteScenePrefab
sprite_scene: (
// SpriteSheetPrefab with index 0
sheet: Sheet(
// TexturePrefab
thread '<unnamed>' panicked at 'Already borrowed mutably: InvalidBorrow', src\libcore\result.rs:997:5
stack backtrace:
0: std::sys::windows::backtrace::set_frames
at /rustc/94fd0458951a4ff91c03366445f0e2e93b86bd2f\/src\libstd\sys\windows\backtrace\mod.rs:95
1: std::sys::windows::backtrace::unwind_backtrace
at /rustc/94fd0458951a4ff91c03366445f0e2e93b86bd2f\/src\libstd\sys\windows\backtrace\mod.rs:82
2: std::sys_common::backtrace::_print
at /rustc/94fd0458951a4ff91c03366445f0e2e93b86bd2f\/src\libstd\sys_common\backtrace.rs:71
3: std::sys_common::backtrace::print
at /rustc/94fd0458951a4ff91c03366445f0e2e93b86bd2f\/src\libstd\sys_common\backtrace.rs:59
error: failed to run custom build command for `survival v0.1.0 (C:\Users\Jasper\IdeaProjects\survival)`
process didn't exit successfully: `C:\Users\Jasper\IdeaProjects\survival\target\release\build\survival-253dead4bb388bc2\build-script-build` (exit code: 101)
--- stderr
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }', src\libcore\result.rs:997:5
stack backtrace:
0: std::sys::windows::backtrace::set_frames
at /rustc/94fd0458951a4ff91c03366445f0e2e93b86bd2f\/src\libstd\sys\windows\backtrace\mod.rs:95
1: std::sys::windows::backtrace::unwind_backtrace
at /rustc/94fd0458951a4ff91c03366445f0e2e93b86bd2f\/src\libstd\sys\windows\backtrace\mod.rs:82
2: std::sys_common::backtrace::_print