From c4812362b9163f96958bf36f9d16e6f159a9e6e7 Mon Sep 17 00:00:00 2001 From: DUDDINO RITARDATO Date: Sun, 6 Oct 2019 17:47:20 +0200 Subject: [PATCH 01/12] implement FromIterator for Box --- src/liballoc/boxed.rs | 9 +++++++++ src/liballoc/tests.rs | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index b2789a535fe49..f7973a236c36c 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -1070,3 +1070,12 @@ impl Future for Box { F::poll(Pin::new(&mut *self), cx) } } + +#[unstable(feature = "box_str_from_iter", issue = "0")] +impl FromIterator for Box { + fn from_iter>(iter: I) -> Box { + let mut buf = String::new(); + buf.extend(iter); + buf.into_boxed_str() + } +} diff --git a/src/liballoc/tests.rs b/src/liballoc/tests.rs index ed46ba8a1b938..1d90e7038bad6 100644 --- a/src/liballoc/tests.rs +++ b/src/liballoc/tests.rs @@ -151,3 +151,12 @@ fn test_array_from_slice() { let a: Result, _> = r.clone().try_into(); assert!(a.is_err()); } + +#[test] +fn box_str_from_iter(){ + let iter = (0..100).map(|_|{'☺'}); + let string: Box = iter.collect(); + + assert_eq!(string.len(), 100); + assert_eq!(string.chars().nth(5), '☺'); +} \ No newline at end of file From a0a313cdde9f23a36cb5b99ff7e0cfbeb90d2ad3 Mon Sep 17 00:00:00 2001 From: DUDDINO RITARDATO Date: Sun, 6 Oct 2019 18:02:16 +0200 Subject: [PATCH 02/12] fixed trailing newline --- src/liballoc/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/tests.rs b/src/liballoc/tests.rs index 1d90e7038bad6..377dcd73a5037 100644 --- a/src/liballoc/tests.rs +++ b/src/liballoc/tests.rs @@ -159,4 +159,4 @@ fn box_str_from_iter(){ assert_eq!(string.len(), 100); assert_eq!(string.chars().nth(5), '☺'); -} \ No newline at end of file +} From 7192b0cba2b5b546f082249689a8bec584965d01 Mon Sep 17 00:00:00 2001 From: DUDDINO RITARDATO Date: Sun, 6 Oct 2019 18:12:13 +0200 Subject: [PATCH 03/12] added use crate::string::String --- src/liballoc/boxed.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index f7973a236c36c..9ae6ab7cc7c3a 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -95,6 +95,7 @@ use core::task::{Context, Poll}; use crate::alloc::{self, Global, Alloc}; use crate::vec::Vec; use crate::raw_vec::RawVec; +use crate::string::String; use crate::str::from_boxed_utf8_unchecked; /// A pointer type for heap allocation. From b2688272e3cc65de5bfb2e5e96fc8988f977c5d0 Mon Sep 17 00:00:00 2001 From: DUDDINO RITARDATO Date: Sun, 6 Oct 2019 18:12:45 +0200 Subject: [PATCH 04/12] added use std::string::String --- src/liballoc/tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/liballoc/tests.rs b/src/liballoc/tests.rs index 377dcd73a5037..410688e5bd579 100644 --- a/src/liballoc/tests.rs +++ b/src/liballoc/tests.rs @@ -9,6 +9,7 @@ use core::f64; use core::i64; use std::boxed::Box; +use std::string::String; #[test] fn test_owned_clone() { From ab689ea1642cda1d7bfadbfadab6b89dda869ad7 Mon Sep 17 00:00:00 2001 From: DUDDINO RITARDATO Date: Sun, 6 Oct 2019 18:54:47 +0200 Subject: [PATCH 05/12] Using String::from_iter --- src/liballoc/boxed.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 9ae6ab7cc7c3a..00b7f2bc2f312 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -1075,8 +1075,6 @@ impl Future for Box { #[unstable(feature = "box_str_from_iter", issue = "0")] impl FromIterator for Box { fn from_iter>(iter: I) -> Box { - let mut buf = String::new(); - buf.extend(iter); - buf.into_boxed_str() + String::from_iter(iter).into_boxed_str() } } From 048710abc3cd68d7fd79b1e0c252fc129761093e Mon Sep 17 00:00:00 2001 From: DUDDINO RITARDATO Date: Sun, 6 Oct 2019 20:32:31 +0200 Subject: [PATCH 06/12] fixed test --- src/liballoc/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/tests.rs b/src/liballoc/tests.rs index 410688e5bd579..f60b51feb069c 100644 --- a/src/liballoc/tests.rs +++ b/src/liballoc/tests.rs @@ -159,5 +159,5 @@ fn box_str_from_iter(){ let string: Box = iter.collect(); assert_eq!(string.len(), 100); - assert_eq!(string.chars().nth(5), '☺'); + assert_eq!(string.chars().nth(5), Some('☺')); } From 0fad2a6c1236db2ee86198baa2c45de747331cc1 Mon Sep 17 00:00:00 2001 From: DUDDINO RITARDATO Date: Sun, 6 Oct 2019 22:02:20 +0200 Subject: [PATCH 07/12] fixed unused import --- src/liballoc/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/liballoc/tests.rs b/src/liballoc/tests.rs index f60b51feb069c..b0dcad49bbb55 100644 --- a/src/liballoc/tests.rs +++ b/src/liballoc/tests.rs @@ -9,7 +9,6 @@ use core::f64; use core::i64; use std::boxed::Box; -use std::string::String; #[test] fn test_owned_clone() { From c73c4b4dd7a8223d7b4c5df15cbea31e432efdaf Mon Sep 17 00:00:00 2001 From: DUDDINO RITARDATO Date: Sun, 6 Oct 2019 23:51:17 +0200 Subject: [PATCH 08/12] changed string.len() into string.chars().count() --- src/liballoc/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/tests.rs b/src/liballoc/tests.rs index b0dcad49bbb55..831dc6db1dc8c 100644 --- a/src/liballoc/tests.rs +++ b/src/liballoc/tests.rs @@ -157,6 +157,6 @@ fn box_str_from_iter(){ let iter = (0..100).map(|_|{'☺'}); let string: Box = iter.collect(); - assert_eq!(string.len(), 100); + assert_eq!(string.chars().count(), 100); assert_eq!(string.chars().nth(5), Some('☺')); } From f2f9eecc2dfec7943fe1f7dd1c39788df1ed948d Mon Sep 17 00:00:00 2001 From: Duddino <47313600+Duddino@users.noreply.github.com> Date: Fri, 18 Oct 2019 18:25:34 +0200 Subject: [PATCH 09/12] Making the implementation stable --- src/liballoc/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 00b7f2bc2f312..33c33b1d0667d 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -1072,7 +1072,7 @@ impl Future for Box { } } -#[unstable(feature = "box_str_from_iter", issue = "0")] +#[stable(feature = "box_str_from_iter", issue = "0", since = "1.39.0")] impl FromIterator for Box { fn from_iter>(iter: I) -> Box { String::from_iter(iter).into_boxed_str() From b10277a26f24385b5178e7db79e4f67135838cf1 Mon Sep 17 00:00:00 2001 From: Duddino <47313600+Duddino@users.noreply.github.com> Date: Sat, 19 Oct 2019 13:57:13 +0200 Subject: [PATCH 10/12] since 1.40.0 --- src/liballoc/boxed.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 33c33b1d0667d..2b6624a53c1b5 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -1072,7 +1072,7 @@ impl Future for Box { } } -#[stable(feature = "box_str_from_iter", issue = "0", since = "1.39.0")] +#[stable(feature = "box_str_from_iter", issue = "0", since = "1.40.0")] impl FromIterator for Box { fn from_iter>(iter: I) -> Box { String::from_iter(iter).into_boxed_str() From e773b204caf376631601f74c28a2e78452a053c9 Mon Sep 17 00:00:00 2001 From: Duddino <47313600+Duddino@users.noreply.github.com> Date: Sat, 19 Oct 2019 14:09:01 +0200 Subject: [PATCH 11/12] removed issue --- src/liballoc/boxed.rs | 125 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 124 insertions(+), 1 deletion(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 2b6624a53c1b5..05bb643c6076e 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -6,6 +6,129 @@ //! //! # Examples //! +1 +//! A pointer type for heap allocation. +2 +//! +3 +//! [`Box`], casually referred to as a 'box', provides the simplest form of +4 +//! heap allocation in Rust. Boxes provide ownership for this allocation, and +5 +//! drop their contents when they go out of scope. +6 +//! +7 +//! # Examples +8 +//! +9 +//! Move a value from the stack to the heap by creating a [`Box`]: +10 +//! +11 +//! ``` +12 +//! let val: u8 = 5; +13 +//! let boxed: Box = Box::new(val); +14 +//! ``` +15 +//! +16 +//! Move a value from a [`Box`] back to the stack by [dereferencing]: +17 +//! +18 +//! ``` +19 +//! let boxed: Box = Box::new(5); +20 +//! let val: u8 = *boxed; +21 +//! ``` +22 +//! +23 +//! Creating a recursive data structure: +24 +//! +25 +//! ``` +26 +//! #[derive(Debug)] +27 +//! enum List { +28 +//! Cons(T, Box>), +29 +//! Nil, +30 +//! } +31 +//! +32 +//! let list: List = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil)))); +33 +//! println!("{:?}", list); +34 +//! ``` +35 +//! +36 +//! This will print `Cons(1, Cons(2, Nil))`. +37 +//! +38 +//! Recursive structures must be boxed, because if the definition of `Cons` +39 +//! looked like this: +40 +//! +41 +//! ```compile_fail,E0072 +42 +//! # enum List { +43 +//! Cons(T, List), +44 +//! # } +45 +//! ``` +46 +//! +47 +//! It wouldn't work. This is because the size of a `List` depends on how many +48 +//! elements are in the list, and so we don't know how much memory to allocate +49 +//! for a `Cons`. By introducing a [`Box`], which has a defined size, we know how +50 +//! big `Cons` needs to be. +51 +//! +52 +//! # Memory layout +53 +//! +54 +//! For non-zero-sized values, a [`Box`] will use the [`Global`] allocator for +55 +//! its allocation. It is valid to convert both ways between a [`Box`] and a +56 +//! raw pointer allocated with the [`Global`] allocator, given that the +57 +//! [`Layout`] used with the allocator is correct for the type. More precisely, +58 +//! a `value: *mut T` that has been allocated with the [`Global`] allocator +59 +//! with `Layout::for_value(&*value)` may be converted into a box using +60 +//! [`Box::::from_raw(value)`]. Conversely, the memory backing a `value: *mut +61 +//! T` obtained from [`Box::::into_raw`] may be deallocated using the + //! Move a value from the stack to the heap by creating a [`Box`]: //! //! ``` @@ -1072,7 +1195,7 @@ impl Future for Box { } } -#[stable(feature = "box_str_from_iter", issue = "0", since = "1.40.0")] +#[stable(feature = "box_str_from_iter", since = "1.40.0")] impl FromIterator for Box { fn from_iter>(iter: I) -> Box { String::from_iter(iter).into_boxed_str() From 4ddbf8d9fecadbf1d2efd8c3810bc8caa8986989 Mon Sep 17 00:00:00 2001 From: Duddino <47313600+Duddino@users.noreply.github.com> Date: Fri, 1 Nov 2019 09:25:25 +0100 Subject: [PATCH 12/12] removed whitespaces --- src/liballoc/boxed.rs | 61 ------------------------------------------- 1 file changed, 61 deletions(-) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 05bb643c6076e..3b82844202b7e 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -6,127 +6,66 @@ //! //! # Examples //! -1 //! A pointer type for heap allocation. -2 //! -3 //! [`Box`], casually referred to as a 'box', provides the simplest form of -4 //! heap allocation in Rust. Boxes provide ownership for this allocation, and -5 //! drop their contents when they go out of scope. -6 //! -7 //! # Examples -8 //! -9 //! Move a value from the stack to the heap by creating a [`Box`]: -10 //! -11 //! ``` -12 //! let val: u8 = 5; -13 //! let boxed: Box = Box::new(val); -14 //! ``` -15 //! -16 //! Move a value from a [`Box`] back to the stack by [dereferencing]: -17 //! -18 //! ``` -19 //! let boxed: Box = Box::new(5); -20 //! let val: u8 = *boxed; -21 //! ``` -22 //! -23 //! Creating a recursive data structure: -24 //! -25 //! ``` -26 //! #[derive(Debug)] -27 //! enum List { -28 //! Cons(T, Box>), -29 //! Nil, -30 //! } -31 //! -32 //! let list: List = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil)))); -33 //! println!("{:?}", list); -34 //! ``` -35 //! -36 //! This will print `Cons(1, Cons(2, Nil))`. -37 //! -38 //! Recursive structures must be boxed, because if the definition of `Cons` -39 //! looked like this: -40 //! -41 //! ```compile_fail,E0072 -42 //! # enum List { -43 //! Cons(T, List), -44 //! # } -45 //! ``` -46 //! -47 //! It wouldn't work. This is because the size of a `List` depends on how many -48 //! elements are in the list, and so we don't know how much memory to allocate -49 //! for a `Cons`. By introducing a [`Box`], which has a defined size, we know how -50 //! big `Cons` needs to be. -51 //! -52 //! # Memory layout -53 //! -54 //! For non-zero-sized values, a [`Box`] will use the [`Global`] allocator for -55 //! its allocation. It is valid to convert both ways between a [`Box`] and a -56 //! raw pointer allocated with the [`Global`] allocator, given that the -57 //! [`Layout`] used with the allocator is correct for the type. More precisely, -58 //! a `value: *mut T` that has been allocated with the [`Global`] allocator -59 //! with `Layout::for_value(&*value)` may be converted into a box using -60 //! [`Box::::from_raw(value)`]. Conversely, the memory backing a `value: *mut -61 //! T` obtained from [`Box::::into_raw`] may be deallocated using the //! Move a value from the stack to the heap by creating a [`Box`]: