Skip to content

fix: return types in ffi #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
let mut build = cc::Build::new();
build
.file("./deps/ada.cpp")
.file("./deps/ada.h")
.include("./deps/ada.h")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is the root cause of the linking error

.include("./deps/ada_c.h");

let compile_target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS");
Expand Down
52 changes: 33 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ptr;

use thiserror::Error;

pub mod ffi {
Expand All @@ -16,8 +17,8 @@ pub mod ffi {
pub length: usize,
}

impl AsRef<str> for ada_string {
fn as_ref(&self) -> &str {
impl ada_string {
pub fn as_str(self) -> &'static str {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is internal API, and the lifetime of &str is aligned with ada_url, so it's safe to mark it as 'static here

unsafe {
let slice = std::slice::from_raw_parts(self.data.cast(), self.length);
std::str::from_utf8_unchecked(slice)
Expand Down Expand Up @@ -61,7 +62,7 @@ pub mod ffi {
pub fn ada_get_url_components(url: *mut ada_url) -> ada_url_components;

// Getters
pub fn ada_get_origin(url: *mut ada_url) -> ada_owned_string;
pub fn ada_get_origin(url: *mut ada_url) -> *mut ada_owned_string;
pub fn ada_get_href(url: *mut ada_url) -> ada_string;
pub fn ada_get_username(url: *mut ada_url) -> ada_string;
pub fn ada_get_password(url: *mut ada_url) -> ada_string;
Expand Down Expand Up @@ -112,10 +113,12 @@ pub struct Url {

impl Drop for Url {
fn drop(&mut self) {
unsafe {
if let Some(origin) = self.origin {
if let Some(origin) = self.origin {
unsafe {
ffi::ada_free_owned_string(origin);
}
}
unsafe {
ffi::ada_free(self.url);
}
}
Expand All @@ -126,62 +129,63 @@ impl Url {
unsafe {
ffi::ada_can_parse(
input.as_ptr().cast(),
base.unwrap_or_else(ptr::null()).as_ptr().cast(),
base.map(|b| b.as_ptr()).unwrap_or(ptr::null_mut()).cast(),
)
}
}

pub fn origin(&mut self) -> &str {
unsafe {
self.origin = ffi::ada_get_origin(self.url);
return self.origin.as_ref();
self.origin = Some(ffi::ada_get_origin(self.url));
self.origin.map(|o| (*o).as_ref()).unwrap_or("")
}
}

pub fn href(&self) -> &str {
unsafe { ffi::ada_get_href(self.url).as_ref() }
unsafe { ffi::ada_get_href(self.url) }.as_str()
}

pub fn username(&self) -> &str {
unsafe { ffi::ada_get_username(self.url).as_ref() }
unsafe { ffi::ada_get_username(self.url) }.as_str()
}

pub fn password(&self) -> &str {
unsafe { ffi::ada_get_password(self.url).as_ref() }
unsafe { ffi::ada_get_password(self.url) }.as_str()
}

pub fn port(&self) -> &str {
unsafe { ffi::ada_get_port(self.url).as_ref() }
unsafe { ffi::ada_get_port(self.url) }.as_str()
}

pub fn hash(&self) -> &str {
unsafe { ffi::ada_get_hash(self.url).as_ref() }
unsafe { ffi::ada_get_hash(self.url) }.as_str()
}

pub fn host(&self) -> &str {
unsafe { ffi::ada_get_host(self.url).as_ref() }
unsafe { ffi::ada_get_host(self.url) }.as_str()
}

pub fn hostname(&self) -> &str {
unsafe { ffi::ada_get_hostname(self.url).as_ref() }
unsafe { ffi::ada_get_hostname(self.url) }.as_str()
}

pub fn pathname(&self) -> &str {
unsafe { ffi::ada_get_pathname(self.url).as_ref() }
unsafe { ffi::ada_get_pathname(self.url) }.as_str()
}

pub fn search(&self) -> &str {
unsafe { ffi::ada_get_search(self.url).as_ref() }
unsafe { ffi::ada_get_search(self.url) }.as_str()
}

pub fn protocol(&self) -> &str {
unsafe { ffi::ada_get_protocol(self.url).as_ref() }
unsafe { ffi::ada_get_protocol(self.url) }.as_str()
}
}

pub fn parse<U: AsRef<str>>(url: U) -> Result<Url, Error> {
let url_with_0_terminate = std::ffi::CString::new(url.as_ref()).unwrap();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need a parse(char* string, size_t length) API in ada to avoid memory copy here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the Rust &str is not the null-terminated

unsafe {
let mut url_aggregator = ffi::ada_parse(url.as_ref().as_ptr().cast());
let url_aggregator = ffi::ada_parse(url_with_0_terminate.as_ptr());

if ffi::ada_is_valid(url_aggregator) {
Ok(Url {
Expand All @@ -193,3 +197,13 @@ pub fn parse<U: AsRef<str>>(url: U) -> Result<Url, Error> {
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn should_parse_simple_url() {
assert!(parse("https://google.com").is_ok());
}
}