pub struct FieldText<'string>(/* private fields */);Expand description
A wrapper for &str that is checked to be printable ASCII, which is
defined as not containing control characters in RFC8907 section 3.7.
This type implements TryFrom<&str> and TryFrom<&[u8]>; in both cases,
an invalid argument will be returned as an Err variant.
§Examples
Conversions from &str:
use tacacs_plus_protocol::FieldText;
let valid_ascii = "a string";
assert!(FieldText::try_from(valid_ascii).is_ok());
let beyond_ascii = "💀";
assert!(FieldText::try_from(beyond_ascii).is_err());Conversions from &[u8]:
let valid_slice = b"this is (almost) a string";
assert!(FieldText::try_from(valid_slice.as_slice()).is_ok());
let not_printable = b"all ASCII characters with - oh no! - a\ttab";
assert!(FieldText::try_from(not_printable.as_slice()).is_err());
let invalid_utf8 = [0x80]; // where'd the rest of the codepoint go?
assert!(FieldText::try_from(invalid_utf8.as_slice()).is_err());If the std feature is enabled, the FieldText::from_string_lossy() constructor
is also available in case a .try_into().unwrap() is undesirable:
let already_valid = "all ASCII!";
let valid_text = FieldText::from_string_lossy(String::from(already_valid));
assert_eq!(valid_text, already_valid);
let unicode_fun = "\tsome chars and ✨emojis✨ (and a quote: ')";
let escaped_text = FieldText::from_string_lossy(String::from(unicode_fun));
assert_eq!(escaped_text, "\\tsome chars and \\u{2728}emojis\\u{2728} (and a quote: ')");
// now that escaped_text is valid ASCII, a .try_into().unwrap() should be guaranteed
// not to panic with the escaped string
let _: FieldText<'_> = escaped_text.as_ref().try_into().unwrap();Implementations§
Source§impl FieldText<'_>
impl FieldText<'_>
Sourcepub fn from_string_lossy(string: String) -> FieldText<'static>
Available on crate feature std only.
pub fn from_string_lossy(string: String) -> FieldText<'static>
std only.Creates a FieldText from a String, escaping any non-printable-ASCII
characters as necessary.
Sourcepub fn into_owned(self) -> FieldText<'static>
Available on crate feature std only.
pub fn into_owned(self) -> FieldText<'static>
std only.Converts this FieldText to one that owns its underlying data,
extending its lifetime to 'static.
Trait Implementations§
Source§impl<'string> Ord for FieldText<'string>
impl<'string> Ord for FieldText<'string>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<'string> PartialOrd for FieldText<'string>
impl<'string> PartialOrd for FieldText<'string>
impl<'string> Eq for FieldText<'string>
impl<'string> StructuralPartialEq for FieldText<'string>
Auto Trait Implementations§
impl<'string> Freeze for FieldText<'string>
impl<'string> RefUnwindSafe for FieldText<'string>
impl<'string> Send for FieldText<'string>
impl<'string> Sync for FieldText<'string>
impl<'string> Unpin for FieldText<'string>
impl<'string> UnwindSafe for FieldText<'string>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more