1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-10 17:29:36 +00:00

document Path::unprocessed panic

This commit is contained in:
Rob Ede 2022-01-19 18:33:23 +00:00
parent cb5d9a7e64
commit 3dd98c308c
No known key found for this signature in database
GPG key ID: 97C636207D3EF933
3 changed files with 20 additions and 7 deletions

View file

@ -49,7 +49,7 @@ impl<T: ResourcePath> Path<T> {
&mut self.path &mut self.path
} }
/// Path. /// Returns unprocessed part of the path.
#[inline] #[inline]
pub fn path(&self) -> &str { pub fn path(&self) -> &str {
profile_method!(path); profile_method!(path);
@ -63,9 +63,21 @@ impl<T: ResourcePath> Path<T> {
} }
} }
/// Returns unprocessed part of the path.
///
/// # Panics
/// Unlike [`path`](Self::path), this will panic if `skip` indexes further than the path length.
#[inline]
pub fn unprocessed(&self) -> &str {
profile_method!(unprocessed);
&self.path.path()[(self.skip as usize)..]
}
/// Set new path. /// Set new path.
#[inline] #[inline]
pub fn set(&mut self, path: T) { pub fn set(&mut self, path: T) {
profile_method!(set);
self.skip = 0; self.skip = 0;
self.path = path; self.path = path;
self.segments.clear(); self.segments.clear();
@ -74,6 +86,8 @@ impl<T: ResourcePath> Path<T> {
/// Reset state. /// Reset state.
#[inline] #[inline]
pub fn reset(&mut self) { pub fn reset(&mut self) {
profile_method!(reset);
self.skip = 0; self.skip = 0;
self.segments.clear(); self.segments.clear();
} }
@ -81,6 +95,7 @@ impl<T: ResourcePath> Path<T> {
/// Skip first `n` chars in path. /// Skip first `n` chars in path.
#[inline] #[inline]
pub fn skip(&mut self, n: u16) { pub fn skip(&mut self, n: u16) {
profile_method!(skip);
self.skip += n; self.skip += n;
} }
@ -102,6 +117,8 @@ impl<T: ResourcePath> Path<T> {
name: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>,
value: impl Into<Cow<'static, str>>, value: impl Into<Cow<'static, str>>,
) { ) {
profile_method!(add_static);
self.segments self.segments
.push((name.into(), PathItem::Static(value.into()))); .push((name.into(), PathItem::Static(value.into())));
} }
@ -136,11 +153,6 @@ impl<T: ResourcePath> Path<T> {
None None
} }
/// Get unprocessed part of the path
pub fn unprocessed(&self) -> &str {
&self.path.path()[(self.skip as usize)..]
}
/// Get matched parameter by name. /// Get matched parameter by name.
/// ///
/// If keyed parameter is not available empty string is used as default value. /// If keyed parameter is not available empty string is used as default value.

View file

@ -256,6 +256,7 @@ mod tests {
router.path("/name/{val}", 11); router.path("/name/{val}", 11);
let mut router = router.finish(); let mut router = router.finish();
// test skip beyond path length
let mut path = Path::new("/name"); let mut path = Path::new("/name");
path.skip(6); path.skip(6);
assert!(router.recognize_mut(&mut path).is_none()); assert!(router.recognize_mut(&mut path).is_none());

View file

@ -208,7 +208,7 @@ impl HttpRequest {
self.resource_map().url_for(self, name, elements) self.resource_map().url_for(self, name, elements)
} }
/// Generate url for named resource /// Generate URL for named resource
/// ///
/// This method is similar to `HttpRequest::url_for()` but it can be used /// This method is similar to `HttpRequest::url_for()` but it can be used
/// for urls that do not contain variable parts. /// for urls that do not contain variable parts.