Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created January 24, 2020 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/3e43d1895c50ffdd3ccf2825eaba8344 to your computer and use it in GitHub Desktop.
Save rust-play/3e43d1895c50ffdd3ccf2825eaba8344 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::io::{self, BufRead};
struct Migration<'a> {
pub namespace: &'a str,
}
impl<'a> Migration<'a> {
pub fn len(&self) -> usize {
self.namespace.chars().count()
}
}
fn create_migration(namespace: &str) -> *mut () {
let boxed = Box::new(Migration { namespace });
Box::into_raw(boxed) as *mut _
}
fn main() {
let stdin = io::stdin();
let namespace = stdin.lock().lines().next().unwrap().unwrap();
let migration = create_migration(&namespace);
let unboxed: Migration<'_> = unsafe { *Box::from_raw(migration as _) };
println!("{}", unboxed.len());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment