Skip to content

Commit 54d61b8

Browse files
anonrigsteveklabnik
andcommitted
fix: properly clone url
Co-authored-by: Steve Klabnik <[email protected]>
1 parent dbd9201 commit 54d61b8

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

Diff for: deps/ada.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* auto-generated on 2023-08-25 15:25:45 -0400. Do not edit! */
1+
/* auto-generated on 2023-08-26 17:38:28 -0400. Do not edit! */
22
/* begin file src/ada.cpp */
33
#include "ada.h"
44
/* begin file src/checkers.cpp */
@@ -14893,6 +14893,11 @@ void ada_free(ada_url result) noexcept {
1489314893
delete r;
1489414894
}
1489514895

14896+
ada_url ada_copy(ada_url input) noexcept {
14897+
ada::result<ada::url_aggregator>& r = get_instance(input);
14898+
return new ada::result<ada::url_aggregator>(r);
14899+
}
14900+
1489614901
bool ada_is_valid(ada_url result) noexcept {
1489714902
ada::result<ada::url_aggregator>& r = get_instance(result);
1489814903
return r.has_value();

Diff for: deps/ada.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* auto-generated on 2023-08-25 15:25:45 -0400. Do not edit! */
1+
/* auto-generated on 2023-08-26 17:38:28 -0400. Do not edit! */
22
/* begin file include/ada.h */
33
/**
44
* @file ada.h
@@ -6922,14 +6922,14 @@ inline void url_search_params::sort() {
69226922
#ifndef ADA_ADA_VERSION_H
69236923
#define ADA_ADA_VERSION_H
69246924

6925-
#define ADA_VERSION "2.6.2"
6925+
#define ADA_VERSION "2.6.3"
69266926

69276927
namespace ada {
69286928

69296929
enum {
69306930
ADA_VERSION_MAJOR = 2,
69316931
ADA_VERSION_MINOR = 6,
6932-
ADA_VERSION_REVISION = 2,
6932+
ADA_VERSION_REVISION = 3,
69336933
};
69346934

69356935
} // namespace ada

Diff for: deps/ada_c.h

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ bool ada_can_parse_with_base(const char* input, size_t input_length,
5151

5252
void ada_free(ada_url result);
5353
void ada_free_owned_string(ada_owned_string owned);
54+
ada_url ada_copy(ada_url input);
5455

5556
bool ada_is_valid(ada_url result);
5657

Diff for: src/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ extern "C" {
5959
) -> *mut ada_url;
6060
pub fn ada_free(url: *mut ada_url);
6161
pub fn ada_free_owned_string(url: *mut ada_owned_string);
62+
pub fn ada_copy(url: *mut ada_url) -> *mut ada_url;
6263
pub fn ada_is_valid(url: *mut ada_url) -> bool;
6364
pub fn ada_can_parse(url: *const c_char, length: usize) -> bool;
6465
pub fn ada_can_parse_with_base(

Diff for: src/lib.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use std::os::raw::c_uint;
3939
use std::{borrow, fmt, hash, ops};
4040
use thiserror::Error;
4141

42+
extern crate alloc;
4243
#[cfg(feature = "serde")]
4344
extern crate serde;
4445

@@ -68,11 +69,21 @@ impl From<c_uint> for HostType {
6869
}
6970

7071
/// A parsed URL struct according to WHATWG URL specification.
71-
#[derive(Eq, Clone)]
72+
#[derive(Eq)]
7273
pub struct Url {
7374
url: *mut ffi::ada_url,
7475
}
7576

77+
/// Clone trait by default uses bit-wise copy.
78+
/// In Rust, FFI requires deep copy, which requires an additional/inexpensive FFI call.
79+
impl Clone for Url {
80+
fn clone(&self) -> Self {
81+
Url {
82+
url: unsafe { ffi::ada_copy(self.url) },
83+
}
84+
}
85+
}
86+
7687
impl Drop for Url {
7788
fn drop(&mut self) {
7889
unsafe { ffi::ada_free(self.url) }
@@ -749,4 +760,14 @@ mod test {
749760
let deserialized: Url = serde_json::from_str(&output).unwrap();
750761
assert_eq!(deserialized.href(), input.to_string() + "/");
751762
}
763+
764+
#[test]
765+
fn should_clone() {
766+
let first = Url::parse("https://lemire.me", None).unwrap();
767+
let mut second = first.clone();
768+
second.set_href("https://yagiz.co");
769+
assert_ne!(first.href(), second.href());
770+
assert_eq!(first.href(), "https://lemire.me/");
771+
assert_eq!(second.href(), "https://yagiz.co/");
772+
}
752773
}

0 commit comments

Comments
 (0)