From dca9ff9a13b0ca04160828413e4550225fb1e04f Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Wed, 3 Jul 2013 20:02:09 -0700 Subject: [PATCH] std: remove str::NullTerminatedStr --- src/libstd/prelude.rs | 2 +- src/libstd/str.rs | 55 +------------------ .../static-slice-not-null-terminated.rs | 21 ------- 3 files changed, 2 insertions(+), 76 deletions(-) delete mode 100644 src/test/compile-fail/static-slice-not-null-terminated.rs diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index 517bc4a441a..7e21504c1d2 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -63,7 +63,7 @@ pub use path::WindowsPath; pub use ptr::RawPtr; pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr, ToBytesConsume}; -pub use str::{Str, StrVector, StrSlice, OwnedStr, NullTerminatedStr}; +pub use str::{Str, StrVector, StrSlice, OwnedStr}; pub use from_str::{FromStr}; pub use to_bytes::IterBytes; pub use to_str::{ToStr, ToStrConsume}; diff --git a/src/libstd/str.rs b/src/libstd/str.rs index c936c1e25db..2a24351fff4 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1981,35 +1981,6 @@ fn as_c_str(&self, f: &fn(*libc::c_char) -> T) -> T { } } -#[allow(missing_doc)] -pub trait NullTerminatedStr { - fn as_bytes_with_null<'a>(&'a self) -> &'a [u8]; -} - -impl NullTerminatedStr for ~str { - /// Work with the byte buffer of a string as a byte slice. - /// - /// The byte slice does include the null terminator. - #[inline] - fn as_bytes_with_null<'a>(&'a self) -> &'a [u8] { - let ptr: &'a ~[u8] = unsafe { cast::transmute(self) }; - let slice: &'a [u8] = *ptr; - slice - } -} - -impl NullTerminatedStr for @str { - /// Work with the byte buffer of a string as a byte slice. - /// - /// The byte slice does include the null terminator. - #[inline] - fn as_bytes_with_null<'a>(&'a self) -> &'a [u8] { - let ptr: &'a @[u8] = unsafe { cast::transmute(self) }; - let slice: &'a [u8] = *ptr; - slice - } -} - #[allow(missing_doc)] pub trait OwnedStr { fn push_str_no_overallocate(&mut self, rhs: &str); @@ -2979,30 +2950,6 @@ fn test_as_bytes() { assert_eq!("ศไทย中华Việt Nam".as_bytes(), v); } - #[test] - fn test_as_bytes_with_null() { - // has null - let v = [ - 224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228, - 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97, - 109, 0 - ]; - - let s1 = @""; - let s2 = @"abc"; - let s3 = @"ศไทย中华Việt Nam"; - assert_eq!(s1.as_bytes_with_null(), &[0]); - assert_eq!(s2.as_bytes_with_null(), &['a' as u8, 'b' as u8, 'c' as u8, 0]); - assert_eq!(s3.as_bytes_with_null(), v); - - let s1 = ~""; - let s2 = ~"abc"; - let s3 = ~"ศไทย中华Việt Nam"; - assert_eq!(s1.as_bytes_with_null(), &[0]); - assert_eq!(s2.as_bytes_with_null(), &['a' as u8, 'b' as u8, 'c' as u8, 0]); - assert_eq!(s3.as_bytes_with_null(), v); - } - #[test] fn test_to_bytes_with_null() { let s = ~"ศไทย中华Việt Nam"; @@ -3024,7 +2971,7 @@ fn test_as_bytes_fail() { // Don't double free. (I'm not sure if this exercises the // original problem code path anymore.) let s = ~""; - let _bytes = s.as_bytes_with_null(); + let _bytes = s.as_bytes(); fail!(); } diff --git a/src/test/compile-fail/static-slice-not-null-terminated.rs b/src/test/compile-fail/static-slice-not-null-terminated.rs deleted file mode 100644 index 3cfaa57d540..00000000000 --- a/src/test/compile-fail/static-slice-not-null-terminated.rs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -fn main() { - let _ = (~"foo").as_bytes_with_null(); - let _ = (@"foo").as_bytes_with_null(); - - // a plain static slice is null terminated, but such a slice can - // be sliced shorter (i.e. become non-null terminated) and still - // have the static lifetime - let foo: &'static str = "foo"; - let _ = foo.as_bytes_with_null(); - //~^ ERROR does not implement any method in scope named `as_bytes_with_null` -} -- GitLab