Skip to content

Instantly share code, notes, and snippets.

View Verdagon's full-sized avatar

Evan Ovadia Verdagon

View GitHub Profile

Article Skeleton

title options:

  • A Memory Safety Enigma in the Mojo Standard Library
  • Mojo's Dependent Types and Memory Safety

I was doing my normal thing in Mojo, implementing struct extensions, when I came across this function in the standard library:

Article Skeleton

title options:

  • A Memory Safety Enigma in the Mojo Standard Library
  • Mojo's Dependent Types and Memory Safety

I was doing my normal thing in Mojo, implementing struct extensions, when I came across this function in the standard library:

# Article Skeleton
title options:
- A Memory Safety Enigma in the Mojo Standard Library
- Mojo's Dependent Types and Memory Safety
--------
I was doing my normal thing in Mojo, implementing struct extensions, when I came across this function in the standard library:
#!DeriveStructConstructor extern struct Vec<T> {
extern func new() -> Vec<T>;
extern func with_capacity(capacity: usize) -> Vec<T>;
// Skipping (self) method try_with_capacity: Didn't contain whitelisted type
// Skipping (self) method from_raw_parts: Encountered raw pointer type
// Skipping (self) method new_in: Encountered defaulted rune
// Skipping (self) method with_capacity_in: Encountered defaulted rune
// Skipping (self) method try_with_capacity_in: Encountered defaulted rune
// Skipping (self) method from_raw_parts_in: Encountered raw pointer type
// Skipping (self) method into_raw_parts: Encountered raw pointer type
// Seamless C->Rust example: reversing a string
// Import types. More advanced compilers with generics wouldn't need
// to specify these generic args in theory, it would basically be a
// vanilla java import.
#pragma rsuse Str = &str
#pragma rsuse String = std::string::String
#pragma rsuse Chars = core::str::Chars<'static>
#pragma rsuse RevChars = core::iter::Rev<Chars>
// Import methods. More advanced compilers with generics won't need
// Seamless C->Rust example: reversing a string
// Import types. More advanced compilers with generics wouldn't need
// to specify these generic args in theory, it would basically be a
// vanilla java import.
#pragma rsuse Str = &str
#pragma rsuse String = std::string::String
#pragma rsuse Chars = core::str::Chars<'static>
#pragma rsuse RevChars = core::iter::Rev<Chars>
// Import methods. More advanced compilers with generics won't need
// Seamless C->Rust example: reversing a string
// Import types. More advanced compilers with generics wouldn't need
// to specify these generic args in theory, it would basically be a
// vanilla java import.
#pragma rsuse Str = &str
#pragma rsuse String = std::string::String
#pragma rsuse Chars = core::str::Chars<'static>
#pragma rsuse RevChars = core::iter::Rev<Chars>
// Import methods. More advanced compilers with generics won't need
#pragma rsuse Str = &str
#pragma rsuse Str_chars = str::chars
#pragma rsuse String = std::string::String
#pragma rsuse String_as_str = String::as_str
#pragma rsuse String_drop = String::drop
#pragma rsuse Chars = core::str::Chars<'static>
#pragma rsuse Chars_rev = Chars::rev
#pragma rsuse RevChars = core::iter::Rev<Chars>
#pragma rsuse RevChars_collectString = RevChars::collect::<String>
#include <rust_deps/rust_deps.h>
% ./main -m ../mixtral/mixtral-8x7b-instruct-v0.1.Q5_K_M.gguf -t 8 -n 1 -p "<|system|>You are a helpful assistant that only ever answers Yes or No. You answer one word then immediately stop, with nothing else in the response, not even punctuation or space, just one word Yes or no.<|im_end|><|im_start|>Is 11134 an even number? Answer yes or no, nothing else.</s><|assistant|>"
Log start
main: build = 2392 (bb6d00bb)
main: built with Apple clang version 14.0.3 (clang-1403.0.22.14.1) for arm64-apple-darwin23.4.0
main: seed = 1713222923
llama_model_loader: loaded meta data with 26 key-value pairs and 995 tensors from ../mixtral/mixtral-8x7b-instruct-v0.1.Q5_K_M.gguf (version GGUF V3 (latest))
llama_model_loader: Dumping metadata keys/values. Note: KV overrides do not apply in this output.
llama_model_loader: - kv 0: general.architecture str = llama
llama_model_loader: - kv 1: general.name str = mistralai_mixtral-8x7b-instruct-v0.1
l

For example, if we have normal rust function:

pub fn kork(zork: u64) -> bool

We'd generate this Rust wrapper library code:

#[no_mangle]
pub extern "C" fn kork_extern(zork: u64) -> bool {
    inner_lib::kork(zork)