-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.L-unconditional_recursionLint: unconditional_recursionLint: unconditional_recursionT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Error
cargo run
Compiling stack_overflow v0.1.0 (/d/stack_overflow)
Finished dev [unoptimized + debuginfo] target(s) in 7.08s
Running `target/debug/stack_overflow`
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Аварийный останов (стек памяти сброшен на диск)
Rust Version
RUST STABLE:
rustc 1.31.1 (b6c32da9b 2018-12-18)
RUST NIGHTLY:
rustc 1.33.0-nightly (9eac38634 2018-12-31)
Code
use std::fmt::Write;
use std::ops::Deref;
use std::fmt;
//////The structure stores in itself the changeable reference and has to clean data in "drop" of structure.
#[derive(Debug)]
pub struct DebugSliceMutString<'a>(&'a mut String);
//Stack overflow <----
//assert_eq!(&null.my_debug(&mut string).unwrap(), "=");
impl<'a> PartialEq<str> for DebugSliceMutString<'a> {
fn eq(&self, other: &str) -> bool {
self == other
}
}
//Stack overflow <----
//assert_eq!(null.my_debug(&mut string).unwrap(), "=");
impl<'a> PartialEq<&str> for DebugSliceMutString<'a> {
fn eq(&self, other: &&str) -> bool {
self == other
}
}
//If to take 'deref' that there is no overflow of a stack!!
//assert_eq!(*null.my_debug(&mut string).unwrap(), "=");
impl<'a> Deref for DebugSliceMutString<'a> {
type Target = String;
fn deref(&self) -> &String {
self.0
}
}
impl<'a> Drop for DebugSliceMutString<'a> {
fn drop(&mut self) {
self.0.clear()
}
}
//MyTrait
pub trait MyDebug {
fn my_debug<'a>(&self, w: &'a mut String) -> Result<DebugSliceMutString<'a>, fmt::Error>;
}
impl MyDebug for (usize, usize) {
fn my_debug<'a>(&self, w: &'a mut String) -> Result<DebugSliceMutString<'a>, fmt::Error> {
write!(w, "{}={}", self.0, self.1)?;
Ok( DebugSliceMutString(w) )
}
}
//
fn main() {
//The buffer for an exception of frequent reallocations of memory
let mut string = String::with_capacity(9);
//Any other type, in this case (usize, usize) is more convenient
let null = (10, 20);
// !!!!!!!
//thread 'main' has overflowed its stack
//fatal runtime error: stack overflow
//Аварийный останов (стек памяти сброшен на диск)
assert_eq!( null.my_debug(&mut string).unwrap(), "=");
// !!!!!!!
//If to use Deref that there is no overflow of a stack!!
//assert_eq!( *null.my_debug(&mut string).unwrap(), "=");
// !!!!!!!OR
//thread 'main' has overflowed its stack
//fatal runtime error: stack overflow
//Аварийный останов (стек памяти сброшен на диск)
//
//let a = null.my_debug(&mut string).unwrap()
// .eq(&"=");
//
//
println!("string, {}, capacity: {}", string, string.capacity());
}
Run
Eduard-gan
Metadata
Metadata
Assignees
Labels
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.L-unconditional_recursionLint: unconditional_recursionLint: unconditional_recursionT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.