Skip to content

fix: properly clone url #32

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
Aug 27, 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
7 changes: 6 additions & 1 deletion deps/ada.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* auto-generated on 2023-08-25 15:25:45 -0400. Do not edit! */
/* auto-generated on 2023-08-26 17:38:28 -0400. Do not edit! */
/* begin file src/ada.cpp */
#include "ada.h"
/* begin file src/checkers.cpp */
Expand Down Expand Up @@ -14893,6 +14893,11 @@ void ada_free(ada_url result) noexcept {
delete r;
}

ada_url ada_copy(ada_url input) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(input);
return new ada::result<ada::url_aggregator>(r);
}

bool ada_is_valid(ada_url result) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
return r.has_value();
Expand Down
6 changes: 3 additions & 3 deletions deps/ada.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* auto-generated on 2023-08-25 15:25:45 -0400. Do not edit! */
/* auto-generated on 2023-08-26 17:38:28 -0400. Do not edit! */
/* begin file include/ada.h */
/**
* @file ada.h
Expand Down Expand Up @@ -6922,14 +6922,14 @@ inline void url_search_params::sort() {
#ifndef ADA_ADA_VERSION_H
#define ADA_ADA_VERSION_H

#define ADA_VERSION "2.6.2"
#define ADA_VERSION "2.6.3"

namespace ada {

enum {
ADA_VERSION_MAJOR = 2,
ADA_VERSION_MINOR = 6,
ADA_VERSION_REVISION = 2,
ADA_VERSION_REVISION = 3,
};

} // namespace ada
Expand Down
1 change: 1 addition & 0 deletions deps/ada_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ bool ada_can_parse_with_base(const char* input, size_t input_length,

void ada_free(ada_url result);
void ada_free_owned_string(ada_owned_string owned);
ada_url ada_copy(ada_url input);

bool ada_is_valid(ada_url result);

Expand Down
1 change: 1 addition & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ extern "C" {
) -> *mut ada_url;
pub fn ada_free(url: *mut ada_url);
pub fn ada_free_owned_string(url: *mut ada_owned_string);
pub fn ada_copy(url: *mut ada_url) -> *mut ada_url;
pub fn ada_is_valid(url: *mut ada_url) -> bool;
pub fn ada_can_parse(url: *const c_char, length: usize) -> bool;
pub fn ada_can_parse_with_base(
Expand Down
23 changes: 22 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use std::os::raw::c_uint;
use std::{borrow, fmt, hash, ops};
use thiserror::Error;

extern crate alloc;
#[cfg(feature = "serde")]
extern crate serde;

Expand Down Expand Up @@ -68,11 +69,21 @@ impl From<c_uint> for HostType {
}

/// A parsed URL struct according to WHATWG URL specification.
#[derive(Eq, Clone)]
#[derive(Eq)]
pub struct Url {
url: *mut ffi::ada_url,
}

/// Clone trait by default uses bit-wise copy.
/// In Rust, FFI requires deep copy, which requires an additional/inexpensive FFI call.
impl Clone for Url {
fn clone(&self) -> Self {
Url {
url: unsafe { ffi::ada_copy(self.url) },
}
}
}

impl Drop for Url {
fn drop(&mut self) {
unsafe { ffi::ada_free(self.url) }
Expand Down Expand Up @@ -749,4 +760,14 @@ mod test {
let deserialized: Url = serde_json::from_str(&output).unwrap();
assert_eq!(deserialized.href(), input.to_string() + "/");
}

#[test]
fn should_clone() {
let first = Url::parse("https://lemire.me", None).unwrap();
let mut second = first.clone();
second.set_href("https://yagiz.co");
assert_ne!(first.href(), second.href());
assert_eq!(first.href(), "https://lemire.me/");
assert_eq!(second.href(), "https://yagiz.co/");
}
}