[−][src]Trait urid::UriBound
A trait for types that can be identified by a URI.
Every type that can be identified by a URI implements this trait. In most cases, you can use the uri
attribute to implement UriBound
safely and quickly:
use urid::*; // Defining the struct #[uri("urn:urid-example:my-struct")] pub struct MyStruct { a: f32, } // Retrieving the URI assert_eq!("urn:urid-example:my-struct", MyStruct::uri().to_str().unwrap());
However, in some cases, you need to implement UriBound
manually, for example if the URI comes from a generated sys
crate:
use urid::*; // This URI is part of a generated `UriBound` crate: const A_FANCY_URI: &'static [u8] = b"urn:urid-example:fancy-uri\0"; // This struct is part of a safe wrapper crate: struct FancyStruct { _fancy_content: f32, } unsafe impl UriBound for FancyStruct { const URI: &'static [u8] = A_FANCY_URI; } assert_eq!("urn:urid-example:fancy-uri", FancyStruct::uri().to_str().unwrap())
Unsafety
The URI
constant has to contain a null terminator (The \0
character at the end), which is used by C programs to determine the end of the string. If you omit it, other parts of your program may violate memory access rules, which is considered undefined behaviour. Since this can not be statically checked by the compiler, this trait is unsafe to implement manually.
Associated Constants
const URI: &'static [u8]
The URI of the type, safed as a byte slice
Currently, there is no way to express a CStr
in a constant way. Therefore, the URI has to be stored as a null-terminated byte slice.
The slice must be a valid URI and must have the null character, expressed as \0
, at the end. Otherwise, other code might produce a segmentation fault or read a faulty URI while looking for the null character.
Provided methods
fn uri() -> &'static Uri
Construct a CStr
reference to the URI.
Assuming that URI
is correct, this method constructs a CStr
reference from the byte slice referenced by URI
.