Skip to content

Commit e10460c

Browse files
chancedanonrig
authored andcommitted
implements std::ops::Deref<Target=str>, std::fmt::Display, AsRef<str> for Url
1 parent cbe597b commit e10460c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ impl Url {
223223
}
224224

225225
/// Return the parsed version of the URL with all components.
226+
///
226227
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-href)
227228
pub fn href(&self) -> &str {
228229
unsafe { ffi::ada_get_href(self.url) }.as_str()
@@ -446,6 +447,30 @@ impl Url {
446447
pub fn has_search(&self) -> bool {
447448
unsafe { ffi::ada_has_search(self.url) }
448449
}
450+
/// Returns the parsed version of the URL with all components.
451+
///
452+
/// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-href)
453+
pub fn as_str(&self) -> &str {
454+
self.href()
455+
}
456+
}
457+
458+
impl std::ops::Deref for Url {
459+
type Target = str;
460+
fn deref(&self) -> &Self::Target {
461+
self.href()
462+
}
463+
}
464+
impl AsRef<str> for Url {
465+
fn as_ref(&self) -> &str {
466+
self.href()
467+
}
468+
}
469+
470+
impl std::fmt::Display for Url {
471+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
472+
f.write_str(self.href())
473+
}
449474
}
450475

451476
#[cfg(test)]

0 commit comments

Comments
 (0)