| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
use errors::{FromStrError, EmptyStrError, NonAsciiError, InvalidUtf8Slice, InvalidUtf8Array}; |
| 10 |
use utf8_iterators::Utf8Iterator; |
| 11 |
use traits::{CharExt, U8UtfExt}; |
| 12 |
use utf16_char::Utf16Char; |
| 13 |
extern crate core; |
| 14 |
use self::core::{hash, fmt, str, ptr}; |
| 15 |
use self::core::cmp::Ordering; |
| 16 |
use self::core::borrow::Borrow; |
| 17 |
use self::core::ops::Deref; |
| 18 |
use self::core::mem::transmute; |
| 19 |
#[cfg(feature="std")] |
| 20 |
use self::core::iter::FromIterator; |
| 21 |
#[cfg(feature="std")] |
| 22 |
#[allow(deprecated)] |
| 23 |
use std::ascii::AsciiExt; |
| 24 |
#[cfg(feature="ascii")] |
| 25 |
extern crate ascii; |
| 26 |
#[cfg(feature="ascii")] |
| 27 |
use self::ascii::{AsciiChar,ToAsciiChar,ToAsciiCharError}; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#[derive(Default)] |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#[derive(PartialEq,Eq, PartialOrd,Ord)] |
| 37 |
|
| 38 |
#[derive(Clone,Copy)] |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
pub struct Utf8Char { |
| 45 |
bytes: [u8; 4], |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
impl str::FromStr for Utf8Char { |
| 53 |
type Err = FromStrError; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
))); |
| 66 |
|
| 67 |
|
| 68 |
k |
| 69 |
|
| 70 |
fn from_str(s: &str) -> Result<Self, FromStrError> { |
| 71 |
if s.is_empty() { |
| 72 |
Err(FromStrError::Empty) |
| 73 |
} else if s.len() != 1+s.as_bytes()[0].extra_utf8_bytes_unchecked() { |
| 74 |
Err(FromStrError::MultipleCodepoints) |
| 75 |
} else { |
| 76 |
let mut bytes = [0; 4]; |
| 77 |
bytes[..s.len()].copy_from_slice(s.as_bytes()); |
| 78 |
Ok(Utf8Char{bytes: bytes}) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
impl From<Utf16Char> for Utf8Char { |
| 83 |
fn from(utf16: Utf16Char) -> Utf8Char { |
| 84 |
match utf16.to_tuple() { |
| 85 |
(a @ 0...0x00_7f, _) => { |
| 86 |
Utf8Char{ bytes: [a as u8, 0, 0, 0] } |
| 87 |
}, |
| 88 |
(u @ 0...0x07_ff, _) => { |
| 89 |
let b = 0x80 | (u & 0x00_3f) as u8; |
| 90 |
let a = 0xc0 | ((u & 0x07_c0) >> 6) as u8; |
| 91 |
Utf8Char{ bytes: [a, b, 0, 0] } |
| 92 |
}, |
| 93 |
(u, None) => { |
| 94 |
let c = 0x80 | (u & 0x00_3f) as u8; |
| 95 |
let b = 0x80 | ((u & 0x0f_c0) >> 6) as u8; |
| 96 |
let a = 0xe0 | ((u & 0xf0_00) >> 12) as u8; |
| 97 |
Utf8Char{ bytes: [a, b, c, 0] } |
| 98 |
}, |
| 99 |
(f, Some(s)) => { |
| 100 |
let f = f + (0x01_00_00u32 >> 10) as u16; |
| 101 |
let d = 0x80 | (s & 0x00_3f) as u8; |
| 102 |
let c = 0x80 | ((s & 0x03_c0) >> 6) as u8 |
| 103 |
| ((f & 0x00_03) << 4) as u8; |
| 104 |
let b = 0x80 | ((f & 0x00_fc) >> 2) as u8; |
| 105 |
let a = 0xf0 | ((f & 0x07_00) >> 8) as u8; |
| 106 |
Utf8Char{ bytes: [a, b, c, d] } |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
impl From<char> for Utf8Char { |
| 112 |
fn from(c: char) -> Self { |
| 113 |
Utf8Char{ bytes: c.to_utf8_array().0 } |
| 114 |
} |
| 115 |
} |
| 116 |
impl From<Utf8Char> for char { |
| 117 |
fn from(uc: Utf8Char) -> char { |
| 118 |
unsafe{ char::from_utf8_exact_slice_unchecked(&uc.bytes[..uc.len()]) } |
| 119 |
} |
| 120 |
} |
| 121 |
impl IntoIterator for Utf8Char { |
| 122 |
type Item=u8; |
| 123 |
type IntoIter=Utf8Iterator; |
| 124 |
|
| 125 |
fn into_iter(self) -> Utf8Iterator { |
| 126 |
Utf8Iterator::from(self) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#[cfg(feature="std")] |
| 131 |
impl Extend<Utf8Char> for Vec<u8> { |
| 132 |
fn extend<I:IntoIterator<Item=Utf8Char>>(&mut self, iter: I) { |
| 133 |
let iter = iter.into_iter(); |
| 134 |
self.reserve(iter.size_hint().0); |
| 135 |
for u8c in iter { |
| 136 |
|
| 137 |
self.push(u8c.bytes[0]); |
| 138 |
for &extra in &u8c.bytes[1..] { |
| 139 |
if extra != 0 { |
| 140 |
self.push(extra); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
#[cfg(feature="std")] |
| 147 |
impl<'a> Extend<&'a Utf8Char> for Vec<u8> { |
| 148 |
fn extend<I:IntoIterator<Item=&'a Utf8Char>>(&mut self, iter: I) { |
| 149 |
self.extend(iter.into_iter().cloned()) |
| 150 |
} |
| 151 |
} |
| 152 |
#[cfg(feature="std")] |
| 153 |
impl Extend<Utf8Char> for String { |
| 154 |
fn extend<I:IntoIterator<Item=Utf8Char>>(&mut self, iter: I) { |
| 155 |
unsafe { self.as_mut_vec().extend(iter) } |
| 156 |
} |
| 157 |
} |
| 158 |
#[cfg(feature="std")] |
| 159 |
impl<'a> Extend<&'a Utf8Char> for String { |
| 160 |
fn extend<I:IntoIterator<Item=&'a Utf8Char>>(&mut self, iter: I) { |
| 161 |
self.extend(iter.into_iter().cloned()) |
| 162 |
} |
| 163 |
} |
| 164 |
#[cfg(feature="std")] |
| 165 |
impl FromIterator<Utf8Char> for String { |
| 166 |
fn from_iter<I:IntoIterator<Item=Utf8Char>>(iter: I) -> String { |
| 167 |
let mut string = String::new(); |
| 168 |
string.extend(iter); |
| 169 |
return string; |
| 170 |
} |
| 171 |
} |
| 172 |
#[cfg(feature="std")] |
| 173 |
impl<'a> FromIterator<&'a Utf8Char> for String { |
| 174 |
fn from_iter<I:IntoIterator<Item=&'a Utf8Char>>(iter: I) -> String { |
| 175 |
iter.into_iter().cloned().collect() |
| 176 |
} |
| 177 |
} |
| 178 |
#[cfg(feature="std")] |
| 179 |
impl FromIterator<Utf8Char> for Vec<u8> { |
| 180 |
fn from_iter<I:IntoIterator<Item=Utf8Char>>(iter: I) -> Self { |
| 181 |
iter.into_iter().collect::<String>().into_bytes() |
| 182 |
} |
| 183 |
} |
| 184 |
#[cfg(feature="std")] |
| 185 |
impl<'a> FromIterator<&'a Utf8Char> for Vec<u8> { |
| 186 |
fn from_iter<I:IntoIterator<Item=&'a Utf8Char>>(iter: I) -> Self { |
| 187 |
iter.into_iter().cloned().collect::<String>().into_bytes() |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
impl AsRef<[u8]> for Utf8Char { |
| 196 |
fn as_ref(&self) -> &[u8] { |
| 197 |
&self.bytes[..self.len()] |
| 198 |
} |
| 199 |
} |
| 200 |
impl AsRef<str> for Utf8Char { |
| 201 |
fn as_ref(&self) -> &str { |
| 202 |
unsafe{ str::from_utf8_unchecked( self.as_ref() ) } |
| 203 |
} |
| 204 |
} |
| 205 |
impl Borrow<[u8]> for Utf8Char { |
| 206 |
fn borrow(&self) -> &[u8] { |
| 207 |
self.as_ref() |
| 208 |
} |
| 209 |
} |
| 210 |
impl Borrow<str> for Utf8Char { |
| 211 |
fn borrow(&self) -> &str { |
| 212 |
self.as_ref() |
| 213 |
} |
| 214 |
} |
| 215 |
impl Deref for Utf8Char { |
| 216 |
type Target = str; |
| 217 |
fn deref(&self) -> &Self::Target { |
| 218 |
self.as_ref() |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
#[cfg(feature="std")] |
| 227 |
#[allow(deprecated)] |
| 228 |
impl AsciiExt for Utf8Char { |
| 229 |
type Owned = Utf8Char; |
| 230 |
fn is_ascii(&self) -> bool { |
| 231 |
self.bytes[0].is_ascii() |
| 232 |
} |
| 233 |
fn eq_ignore_ascii_case(&self, other: &Self) -> bool { |
| 234 |
if self.is_ascii() {self.bytes[0].eq_ignore_ascii_case(&other.bytes[0])} |
| 235 |
else {self == other} |
| 236 |
} |
| 237 |
fn to_ascii_uppercase(&self) -> Self::Owned { |
| 238 |
let mut uc = *self; |
| 239 |
uc.make_ascii_uppercase(); |
| 240 |
uc |
| 241 |
} |
| 242 |
fn to_ascii_lowercase(&self) -> Self::Owned { |
| 243 |
let mut uc = *self; |
| 244 |
uc.make_ascii_lowercase(); |
| 245 |
uc |
| 246 |
} |
| 247 |
fn make_ascii_uppercase(&mut self) { |
| 248 |
self.bytes[0].make_ascii_uppercase() |
| 249 |
} |
| 250 |
fn make_ascii_lowercase(&mut self) { |
| 251 |
self.bytes[0].make_ascii_lowercase(); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
#[cfg(feature="ascii")] |
| 256 |
|
| 257 |
impl From<AsciiChar> for Utf8Char { |
| 258 |
fn from(ac: AsciiChar) -> Self { |
| 259 |
Utf8Char{ bytes: [ac.as_byte(),0,0,0] } |
| 260 |
} |
| 261 |
} |
| 262 |
#[cfg(feature="ascii")] |
| 263 |
|
| 264 |
impl ToAsciiChar for Utf8Char { |
| 265 |
fn to_ascii_char(self) -> Result<AsciiChar, ToAsciiCharError> { |
| 266 |
self.bytes[0].to_ascii_char() |
| 267 |
} |
| 268 |
unsafe fn to_ascii_char_unchecked(self) -> AsciiChar { |
| 269 |
self.bytes[0].to_ascii_char_unchecked() |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
impl hash::Hash for Utf8Char { |
| 278 |
fn hash<H : hash::Hasher>(&self, state: &mut H) { |
| 279 |
self.to_char().hash(state); |
| 280 |
} |
| 281 |
} |
| 282 |
impl fmt::Debug for Utf8Char { |
| 283 |
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result { |
| 284 |
fmt::Debug::fmt(&self.to_char(), fmtr) |
| 285 |
} |
| 286 |
} |
| 287 |
impl fmt::Display for Utf8Char { |
| 288 |
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result { |
| 289 |
fmtr.write_str(self.as_str()) |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
impl PartialEq<char> for Utf8Char { |
| 298 |
fn eq(&self, u32c: &char) -> bool { |
| 299 |
*self == Utf8Char::from(*u32c) |
| 300 |
} |
| 301 |
} |
| 302 |
impl PartialEq<Utf8Char> for char { |
| 303 |
fn eq(&self, u8c: &Utf8Char) -> bool { |
| 304 |
Utf8Char::from(*self) == *u8c |
| 305 |
} |
| 306 |
} |
| 307 |
impl PartialOrd<char> for Utf8Char { |
| 308 |
fn partial_cmp(&self, u32c: &char) -> Option<Ordering> { |
| 309 |
self.partial_cmp(&Self::from(*u32c)) |
| 310 |
} |
| 311 |
} |
| 312 |
impl PartialOrd<Utf8Char> for char { |
| 313 |
fn partial_cmp(&self, u8c: &Utf8Char) -> Option<Ordering> { |
| 314 |
Utf8Char::from(*self).partial_cmp(u8c) |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
impl PartialEq<Utf16Char> for Utf8Char { |
| 319 |
fn eq(&self, u16c: &Utf16Char) -> bool { |
| 320 |
*self == Self::from(*u16c) |
| 321 |
} |
| 322 |
} |
| 323 |
impl PartialOrd<Utf16Char> for Utf8Char { |
| 324 |
fn partial_cmp(&self, u16c: &Utf16Char) -> Option<Ordering> { |
| 325 |
self.partial_cmp(&Self::from(*u16c)) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
impl PartialEq<u8> for Utf8Char { |
| 345 |
fn eq(&self, byte: &u8) -> bool { |
| 346 |
self.bytes[0] == *byte && self.bytes[1] == 0 |
| 347 |
} |
| 348 |
} |
| 349 |
#[cfg(feature = "ascii")] |
| 350 |
|
| 351 |
impl PartialEq<AsciiChar> for Utf8Char { |
| 352 |
#[inline] |
| 353 |
fn eq(&self, ascii: &AsciiChar) -> bool { |
| 354 |
self.bytes[0] == *ascii as u8 |
| 355 |
} |
| 356 |
} |
| 357 |
#[cfg(feature = "ascii")] |
| 358 |
|
| 359 |
impl PartialEq<Utf8Char> for AsciiChar { |
| 360 |
#[inline] |
| 361 |
fn eq(&self, u8c: &Utf8Char) -> bool { |
| 362 |
u8c == self |
| 363 |
} |
| 364 |
} |
| 365 |
#[cfg(feature = "ascii")] |
| 366 |
|
| 367 |
impl PartialOrd<AsciiChar> for Utf8Char { |
| 368 |
#[inline] |
| 369 |
fn partial_cmp(&self, ascii: &AsciiChar) -> Option<Ordering> { |
| 370 |
self.bytes[0].partial_cmp(ascii) |
| 371 |
} |
| 372 |
} |
| 373 |
#[cfg(feature = "ascii")] |
| 374 |
|
| 375 |
impl PartialOrd<Utf8Char> for AsciiChar { |
| 376 |
#[inline] |
| 377 |
fn partial_cmp(&self, u8c: &Utf8Char) -> Option<Ordering> { |
| 378 |
self.partial_cmp(&u8c.bytes[0]) |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
impl Utf8Char { |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
))); |
| 399 |
k |
| 400 |
|
| 401 |
|
| 402 |
pub fn from_str_start(src: &str) -> Result<(Self,usize),EmptyStrError> { |
| 403 |
unsafe { |
| 404 |
if src.is_empty() { |
| 405 |
Err(EmptyStrError) |
| 406 |
} else { |
| 407 |
Ok(Utf8Char::from_slice_start_unchecked(src.as_bytes())) |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
pub fn from_slice_start(src: &[u8]) -> Result<(Self,usize),InvalidUtf8Slice> { |
| 437 |
char::from_utf8_slice_start(src).map(|(_,len)| { |
| 438 |
let mut bytes = [0; 4]; |
| 439 |
bytes[..len].copy_from_slice(&src[..len]); |
| 440 |
(Utf8Char{ bytes: bytes }, len) |
| 441 |
}) |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
pub unsafe fn from_slice_start_unchecked(src: &[u8]) -> (Self,usize) { |
| 450 |
let len = 1+src.get_unchecked(0).extra_utf8_bytes_unchecked(); |
| 451 |
let mut bytes = [0; 4]; |
| 452 |
ptr::copy_nonoverlapping(src.as_ptr(), &mut bytes[0] as *mut u8, len); |
| 453 |
(Utf8Char{ bytes: bytes }, len) |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
pub fn from_array(utf8: [u8;4]) -> Result<Self,InvalidUtf8Array> { |
| 482 |
unsafe { |
| 483 |
|
| 484 |
try!(char::from_utf8_array(utf8)); |
| 485 |
let extra = utf8[0].extra_utf8_bytes_unchecked() as u32; |
| 486 |
|
| 487 |
|
| 488 |
let mask = u32::from_le(0xff_ff_ff_ff >> 8*(3-extra)); |
| 489 |
let unused_zeroed = mask & transmute::<_,u32>(utf8); |
| 490 |
Ok(Utf8Char{ bytes: transmute(unused_zeroed) }) |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
#[inline] |
| 501 |
pub unsafe fn from_array_unchecked(utf8: [u8;4]) -> Self { |
| 502 |
Utf8Char{ bytes: utf8 } |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
pub fn from_ascii(ascii: u8) -> Result<Self,NonAsciiError> { |
| 520 |
if ascii as i8 >= 0 { |
| 521 |
Ok(Utf8Char{ bytes: [ascii, 0, 0, 0] }) |
| 522 |
} else { |
| 523 |
Err(NonAsciiError) |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
#[inline] |
| 533 |
pub unsafe fn from_ascii_unchecked(ascii: u8) -> Self { |
| 534 |
Utf8Char{ bytes: [ascii, 0, 0, 0] } |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
#[inline] |
| 542 |
pub fn len(self) -> usize { |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
(4 - (u32::to_le(unsafe{transmute(self.bytes)})|1).leading_zeros()/8) as usize |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
pub fn is_ascii(&self) -> bool { |
| 569 |
self.bytes[0] <= 127 |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
#[cfg(feature="std")] |
| 575 |
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool { |
| 576 |
if self.is_ascii() {self.bytes[0].eq_ignore_ascii_case(&other.bytes[0])} |
| 577 |
else {self == other} |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
#[cfg(feature="std")] |
| 584 |
pub fn to_ascii_uppercase(&self) -> Self { |
| 585 |
let mut uc = *self; |
| 586 |
uc.make_ascii_uppercase(); |
| 587 |
uc |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
#[cfg(feature="std")] |
| 594 |
pub fn to_ascii_lowercase(&self) -> Self { |
| 595 |
let mut uc = *self; |
| 596 |
uc.make_ascii_lowercase(); |
| 597 |
uc |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
#[inline] |
| 604 |
#[cfg(feature="std")] |
| 605 |
pub fn make_ascii_uppercase(&mut self) { |
| 606 |
self.bytes[0].make_ascii_uppercase() |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
#[inline] |
| 613 |
#[cfg(feature="std")] |
| 614 |
pub fn make_ascii_lowercase(&mut self) { |
| 615 |
self.bytes[0].make_ascii_lowercase(); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
pub fn to_char(self) -> char { |
| 620 |
self.into() |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
pub fn to_slice(self, dst: &mut[u8]) -> usize { |
| 631 |
if self.len() > dst.len() { |
| 632 |
panic!("The provided buffer is too small."); |
| 633 |
} |
| 634 |
dst[..self.len()].copy_from_slice(&self.bytes[..self.len()]); |
| 635 |
self.len() |
| 636 |
} |
| 637 |
|
| 638 |
pub fn to_array(self) -> ([u8;4],usize) { |
| 639 |
(self.bytes, self.len()) |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
pub fn as_str(&self) -> &str { |
| 645 |
self.deref() |
| 646 |
} |
| 647 |
} |