This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ffi' | |
module Scrape | |
extend FFI::Library | |
ffi_lib './target/debug/libscrape.dylib' | |
class TwoNumbers < FFI::Struct | |
layout :first, :int32, | |
:second, :int32 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub struct TwoNumbers { | |
first: i32, | |
second: i32, | |
} | |
#[no_mangle] | |
pub extern fn add_struct_vals(numbers: TwoNumbers) -> i32 { | |
numbers.first + numbers.second | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(libc)] | |
#![feature(cstr_to_str)] | |
#![feature(cstr_memory)] | |
extern crate hyper; | |
extern crate libc; | |
use hyper::Client; | |
use hyper::header::Connection; | |
use std::io::Read; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ffi' | |
module Scrape | |
extend FFI::Library | |
ffi_lib './target/debug/libscrape.dylib' | |
attach_function :get_body, [:string], :string | |
end | |
body = Scrape.get_body("http://doc.rust-lang.org")[191..230] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(slice_chars)] | |
extern crate hyper; | |
use hyper::Client; | |
use hyper::header::Connection; | |
use std::io::Read; | |
fn get_body(url: &str) -> String { | |
let client = Client::new(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn get_body(url: &str) -> String { | |
let client = Client::new(); | |
let temp_resp = client.get(url).header(Connection::close()).send(); | |
let mut resp = temp_resp.unwrap(); | |
} | |
#[test] | |
fn it_works() { | |
assert_eq!( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn get_body(url: String) -> String { | |
} | |
#[test] | |
fn it_works() { | |
assert_eq!( | |
get_body(&"http://www.rust-lang.org".to_string()).slice_chars(73, 105), | |
"<title>The Rust Programming Lang" | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "scrape" | |
version = "0.1.0" | |
authors = ["Mike Piccolo <[email protected]>"] | |
[lib] | |
name = "scrape" | |
crate-type = ["dylib"] | |
[dependencies.hyper] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(libc)] | |
#![feature(cstr_to_str)] | |
#![feature(cstr_memory)] | |
extern crate libc; | |
use std::ffi::{CStr, CString}; | |
#[no_mangle] | |
pub extern fn ruby_reverse(s: *const libc::c_char) -> *const libc::c_char { | |
let str = ruby_string_to_ref_str(s); | |
let string: String = str.chars().rev().collect(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn ruby_string_to_ref_str<'a>(r_string: *const libc::c_char) -> &'a str { | |
unsafe { CStr::from_ptr(r_string) }.to_str().unwrap() | |
} | |
fn string_to_ruby_string(string: String) -> *const libc::c_char { | |
CString::new(string).unwrap().into_ptr() | |
} |