[][src]Struct async_coap::uri::RelRefBuf

pub struct RelRefBuf(_);

Sized, heap-allocated string type guaranteed to contain a well-formed IETF-RFC3986 relative-reference.

The unsized counterpart is RelRef.

This type cannot hold a network path. If this type contains a path that looks like a network path, it will be considered degenerate and you will not be able to losslessly convert it to a UriRef or UriRefBuf. See "Network Path Support" for more details.

This type implements [std::ops::Deref<RelRef>], so you can also use all of the methods from RelRef on this type.

Methods

impl RelRefBuf[src]

pub fn new() -> RelRefBuf[src]

Constructs a new, empty relative reference buffer.

pub fn with_capacity(capacity: usize) -> RelRefBuf[src]

Creates a new, empty RelRefBuf with a capacity of capacity.

pub fn from_str<S>(s: S) -> Result<RelRefBuf, ParseError> where
    S: AsRef<str>, 
[src]

Attempts to create a new RelRefBuf from a string reference.

pub fn from_string(s: String) -> Result<RelRefBuf, ParseError>[src]

Attempts to create a new RelRefBuf from a String.

pub fn from_uri_ref<S>(s: S) -> Option<RelRefBuf> where
    S: AsRef<UriRef>, 
[src]

Attempts to create a new RelRefBuf from a UriRef reference.

pub fn from_rel_ref<S>(s: S) -> RelRefBuf where
    S: AsRef<RelRef>, 
[src]

Attempts to create a new RelRefBuf from a RelRef reference.

impl RelRefBuf[src]

pub fn as_rel_ref(&self) -> &RelRef[src]

Borrows a RelRef slice containing this relative reference.

pub fn as_mut_rel_ref(&mut self) -> &mut RelRef[src]

Borrows a mutable RelRef slice containing this relative reference.

impl RelRefBuf[src]

pub fn disambiguate(&mut self) -> bool[src]

Modifies this relative reference to be unambiguous.

Specifically:

  • this:that is converted to this%3Athat, to avoid confusion with a URI.
  • //this/that is converted to /.//this/that, to avoid confusion with a network path.

Note that ambiguous RelRefBuf instances are always displayed/formatted unambiguously, so there should be little need to ever call this method.

#[must_use] pub fn into_unescape_buf(self) -> UriUnescapeBuf[src]

Experimental: Takes ownership of the RelRefBuf and returns a UriUnescapeBuf that allows for in-line unescaping during iteration of path segments and query items without additional memory allocations.

Example:

use async_coap_uri::prelude::*;
let rel_ref_buf = rel_ref!(unsafe "g:a/b/bl%c3%a5b%c3%a6r?q=g:a&q=foo&q=syltet%c3%b8y").to_rel_ref_buf();
let mut unescape_buf = rel_ref_buf.into_unescape_buf();

let mut query_item_iter = unescape_buf.query_items();

assert_eq!(query_item_iter.next(), Some("q=g:a"));
assert_eq!(query_item_iter.next(), Some("q=foo"));
assert_eq!(query_item_iter.next(), Some("q=syltetøy"));
assert_eq!(query_item_iter.next(), None);

core::mem::drop(query_item_iter);

let mut path_seg_iter = unescape_buf.path_segments();

assert_eq!(path_seg_iter.next(), Some("g:a"));
assert_eq!(path_seg_iter.next(), Some("b"));
assert_eq!(path_seg_iter.next(), Some("blåbær"));
assert_eq!(path_seg_iter.next(), None);

Lifetimes are enforced on the returned path items, meaning the following code does not compile:

This example deliberately fails to compile
    let mut uri = rel_ref!("this:is/fun/bl%c3%a5b%c3%a6rsyltet%c3%b8y").to_owned();
    let first_part;
    let second_part;
    {
        // This line takes ownership of `uri`...
        let mut path_set = uri.into_unescape_buf();
        let mut iter = path_set.path_segments();

        // ...so these string slices will be valid
        // for the lifetime of `iter`.
        first_part = iter.next().unwrap();
        second_part = iter.next().unwrap();
    }
    // Iter has been dropped at this point, but the next
    // line uses both `first_part` and `second_part`---so
    // the compiler will flag an error. If it does compile,
    // it represents a use-after-free error.
    panic!("This should not have compiled! {} {}", first_part, second_part);

pub fn resolve<T>(&mut self, dest: T) where
    T: AsRef<RelRef>, 
[src]

Using this relative-reference as the base, performs "URI-reference resolution" on another relative reference, updating the content of this RelRefBuf with the result.

pub fn clear(&mut self)[src]

Completely clears this RelRefBuf, leaving it empty.

impl RelRefBuf[src]

Unsafe Methods

RelRefBuf needs some unsafe methods in order to function properly. This section is where they are all located.

pub unsafe fn from_string_unchecked(s: String) -> RelRefBuf[src]

Unchecked version of RelRefBuf::from_string.

Safety

This method is marked as unsafe because it allows you to construct a RelRefBuf with a value that is not a well-formed relative-reference.

impl RelRefBuf[src]

pub fn as_str(&self) -> &str[src]

Returns a string slice for this instance.

pub unsafe fn as_mut_str(&mut self) -> &mut str[src]

Returns a mutable string slice (&mut str) for this instance.

Safety

This method is not safe because this type makes guarantees about the structure of the content it contains, which may be violated by using this method.

pub fn as_mut_uri_ref(&mut self) -> &mut UriRef[src]

Borrows a reference to this mutable instance as a mutable URI-Reference (&mut UriRef).

pub fn truncate_heir_part(&mut self)[src]

Removes the authority, path, query, and fragment components, if present.

pub fn truncate_path(&mut self)[src]

Removes the path, query, and fragment components, if present.

pub fn truncate_query(&mut self)[src]

Removes the query, and fragment components, if present.

pub fn truncate_fragment(&mut self)[src]

Removes fragment component, if present.

pub fn truncate_resource(&mut self)[src]

Removes the last path component (up to, but not including, the last slash), along with the query and fragment components, if present.

See UriRefBuf::truncate_resource for more information.

pub fn truncate_last_path_segment(&mut self)[src]

Removes the last path item, along with the query and fragment components, if present.

See UriRefBuf::truncate_last_path_segment for more information.

pub fn add_trailing_slash(&mut self) -> bool[src]

Adds a trailing slash to the path if there isn't a trailing slash already present.

pub fn add_leading_slash(&mut self) -> bool[src]

Adds a leading slash to the path if there isn't one already present.

pub fn push_path_segment(&mut self, segment: &str, trailing_slash: bool)[src]

Percent-encodes and appends the given path segment to this instance, truncating any existing query or fragment in the process.

If this instance isn't empty and doesn't end with a slash, one is first added. A trailing slash will be appended depending on the value of the trailing_slash argument.

pub fn push_query_item(&mut self, item: &str)[src]

Percent-encodes and appends the given query item to this instance, truncating any existing fragment in the process.

If no query is present, the query item is preceded with a '?' to indicate the start of the query component. Otherwise, this method uses & to separate query items.

This method follows the common convention where spaces are encoded as + characters instead of %20.

pub fn push_query_key_value(&mut self, key: &str, value: &str)[src]

Percent-encodes and appends the given query key/value pair to this URI-reference, truncating any existing fragment in the process.

If no query is present, the query item is preceded with a '?' to indicate the start of the query component. Otherwise, this method uses & to separate query items.

This method follows the common convention where spaces are encoded as + characters instead of %20.

Methods from Deref<Target = RelRef>

pub fn to_rel_ref_buf(&self) -> RelRefBuf[src]

Constructs a new RelRefBuf from a &RelRef, disambiguating if degenerate.

pub fn to_uri_ref_buf(&self) -> UriRefBuf[src]

Constructs a new UriRefBuf from a &RelRef, disambiguating if degenerate.

pub const fn as_str(&self) -> &str[src]

Casts this relative reference to a string slice.

pub fn try_as_uri_ref(&self) -> Option<&UriRef>[src]

Casts a non-degenerate relative reference to a &UriRef. Returns None if the relative reference is degenerate.

pub fn as_uri_ref(&self) -> Cow<UriRef>[src]

Returns a [Cow<UriRef>] that usually just contains a reference to this slice, but will contain an owned instance if this relative reference is degenerate.

#[must_use = "this returns a new slice, without modifying the original"] pub fn path_as_rel_ref(&self) -> &RelRef[src]

Trims the query and fragment from this relative reference, leaving only the path.

See also RelRef::trim_query.

#[must_use = "this returns a new slice, without modifying the original"] pub fn query_as_rel_ref(&self) -> Option<&RelRef>[src]

See UriRef::query_as_rel_ref for more information.

#[must_use] pub fn raw_path(&self) -> &str[src]

See UriRef::raw_path for more information.

#[must_use = "this returns a new slice, without modifying the original"] pub fn raw_query(&self) -> Option<&str>[src]

See UriRef::raw_query for more information.

#[must_use = "this returns a new slice, without modifying the original"] pub fn raw_fragment(&self) -> Option<&str>[src]

See UriRef::raw_fragment for more information.

#[must_use] pub fn raw_path_segments(&self) -> impl Iterator<Item = &str>[src]

See UriRef::raw_path_segments for more information.

#[must_use] pub fn raw_query_items(&self) -> impl Iterator<Item = &str>[src]

See UriRef::raw_query_items for more information.

#[must_use] pub fn raw_query_key_values(&self) -> impl Iterator<Item = (&str, &str)>[src]

See UriRef::raw_query_key_values for more information.

#[must_use] pub fn fragment(&self) -> Option<Cow<str>>[src]

See UriRef::fragment for more information.

#[must_use] pub fn path_segments(&self) -> impl Iterator<Item = Cow<str>>[src]

See UriRef::path_segments for more information.

#[must_use] pub fn query_items(&self) -> impl Iterator<Item = Cow<str>>[src]

See UriRef::query_items for more information.

#[must_use] pub fn query_key_values(&self) -> impl Iterator<Item = (Cow<str>, Cow<str>)>[src]

See UriRef::query_key_values for more information.

#[must_use] pub fn has_trailing_slash(&self) -> bool[src]

See UriRef::has_trailing_slash for more information.

#[must_use] pub fn colon_in_first_path_segment(&self) -> Option<usize>[src]

Determines if this RelRef is degenerate specifically because it is a relative path with a colon in the first path segment and no special characters appearing before it.

See the section "RelRef" for more details.

pub fn is_degenerate(&self) -> bool[src]

Determines if this RelRef is degenerate.

See the section "RelRef" for more details.

#[must_use] pub fn resolved_rel_ref<UF>(&self, dest: UF) -> RelRefBuf where
    UF: AsRef<RelRef>, 
[src]

Resolves a relative URI against this relative URI, yielding a new relative URI as a RelRefBuf.

#[must_use = "this returns the trimmed uri as a new slice, without modifying the original"] pub fn trim_fragment(&self) -> &RelRef[src]

Returns this relative reference slice without the fragment component.

#[must_use = "this returns the trimmed uri as a new slice, without modifying the original"] pub fn trim_query(&self) -> &RelRef[src]

Returns this relative reference slice without the query or fragment components.

#[must_use = "this returns the trimmed uri as a new slice, without modifying the original"] pub fn trim_resource(&self) -> &RelRef[src]

See UriRef::trim_resource for more information.

#[must_use = "this returns the trimmed uri as a new slice, without modifying the original"] pub fn trim_trailing_slash(&self) -> &RelRef[src]

Removes any trailing slash that might be at the end of the path, along with the query and fragment.

If the path consists of a single slash ("/"), then it is not removed.

Examples

use async_coap_uri::prelude::*;
assert_eq!(rel_ref!("a").trim_trailing_slash(),               rel_ref!("a"));
assert_eq!(rel_ref!("a/b/c/?blah#frag").trim_trailing_slash(),rel_ref!("a/b/c"));
assert_eq!(rel_ref!("/").trim_trailing_slash(),               rel_ref!("/"));
assert_eq!(rel_ref!(unsafe "//").trim_trailing_slash(),       rel_ref!("/"));
assert_eq!(rel_ref!(unsafe "//foo/?bar").trim_trailing_slash(),rel_ref!(unsafe "//foo"));

Note that the behavior of this method is different than the behavior for UriRef::trim_trailing_slash: "//" is considered to be a path starting with two slashes rather than a network path with an empty authority and an empty path:

assert_eq!(rel_ref!(unsafe "//").trim_trailing_slash(),    rel_ref!("/"));
assert_eq!(rel_ref!(unsafe "///").trim_trailing_slash(),   rel_ref!(unsafe "//"));
assert_eq!(rel_ref!(unsafe "////").trim_trailing_slash(),  rel_ref!(unsafe "///"));

#[must_use = "this returns the trimmed uri as a new slice, without modifying the original"] pub fn trim_leading_slashes(&self) -> &RelRef[src]

Returns this relative reference slice without any leading slashes.

#[must_use = "this returns the trimmed uri as a new slice, without modifying the original"] pub fn trim_leading_dot_slashes(&self) -> &RelRef[src]

Returns this relative reference slice without any leading instances of "./" or "/.".

#[must_use = "this returns the leading path item trimmed uri as a new slice, without modifying the original"] pub fn trim_leading_path_segment(&self) -> (&str, &RelRef)[src]

Returns this relative reference slice without its first path segment.

#[must_use = "this returns the trimmed uri as a new slice, without modifying the original"] pub fn trim_leading_n_path_segments(&self, n: usize) -> (&str, &RelRef)[src]

Returns a tuple with a string slice contianing the first n path segments and a &RelRef containing the rest of the relative reference.

#[must_use = "this returns the trimmed uri as a new slice, without modifying the original"] pub fn trim_to_shorten(&self, base: &RelRef) -> Option<&RelRef>[src]

Attempts to return a shortened version of this relative reference that is relative to base.

pub const unsafe fn as_uri_ref_unchecked(&self) -> &UriRef[src]

Directly converts this &RelRef to a &UriRef, without performing the checks that as_uri_ref() does.

This is unsafe for the reasons described here.

Trait Implementations

impl AsRef<RelRef> for RelRefBuf[src]

impl AsRef<UriRefBuf> for RelRefBuf[src]

impl AsRef<String> for RelRefBuf[src]

impl AsRef<RelRefBuf> for RelRefBuf[src]

impl AsRef<str> for RelRefBuf[src]

impl Display for RelRefBuf[src]

impl Eq for RelRefBuf[src]

impl Ord for RelRefBuf[src]

fn max(self, other: Self) -> Self1.21.0[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self1.21.0[src]

Compares and returns the minimum of two values. Read more

fn clamp(self, min: Self, max: Self) -> Self[src]

🔬 This is a nightly-only experimental API. (clamp)

Restrict a value to a certain interval. Read more

impl<T> PartialOrd<T> for RelRefBuf where
    T: AsRef<str> + ?Sized
[src]

#[must_use] fn lt(&self, other: &Rhs) -> bool1.0.0[src]

This method tests less than (for self and other) and is used by the < operator. Read more

#[must_use] fn le(&self, other: &Rhs) -> bool1.0.0[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use] fn gt(&self, other: &Rhs) -> bool1.0.0[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use] fn ge(&self, other: &Rhs) -> bool1.0.0[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl Deref for RelRefBuf[src]

type Target = RelRef

The resulting type after dereferencing.

impl Debug for RelRefBuf[src]

impl Clone for RelRefBuf[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl AnyUriRef for RelRefBuf[src]

#[must_use] fn display(&self) -> UriDisplay<Self>[src]

Wraps this AnyUriRef instance in a [UriDisplay] object for use with formatting macros like write! and format!. Read more

impl Default for RelRefBuf[src]

impl Hash for RelRefBuf[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<'_> From<&'_ RelRefBuf> for RelRefBuf[src]

impl From<RelRefBuf> for UriRefBuf[src]

impl<'_> From<&'_ RelRef> for RelRefBuf[src]

impl Borrow<RelRef> for RelRefBuf[src]

impl<T> PartialEq<T> for RelRefBuf where
    T: AsRef<str> + ?Sized
[src]

#[must_use] fn ne(&self, other: &Rhs) -> bool1.0.0[src]

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,