This file contains 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 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 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 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 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 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 <mfpiccolo@gmail.com>"] | |
[lib] | |
name = "scrape" | |
crate-type = ["dylib"] | |
[dependencies.hyper] |
This file contains 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 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() | |
} |
This file contains 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 rust_cstr = unsafe { CStr::from_ptr(s) }; // &std::ffi::c_str::CStr | |
let str = rust_cstr.to_str().unwrap(); // &str | |
let string: String = str.chars().rev().collect(); // collections::string::String |
This file contains 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
extern crate libc; | |
use std::ffi::CStr; | |
pub extern fn ruby_reverse(s: *const libc::c_char) -> *const libc::c_char { | |
let rust_cstr = unsafe { CStr::from_ptr(s) }; // &std::ffi::c_str::CStr | |
let str = rust_cstr.to_str().unwrap(); // &str | |
let string: String = str.chars().rev().collect(); // collections::string::String | |
// convert and return for ffi ruby module | |
} |