I have implemented as you have shown and getting :
use std::fmt::Debug;
| ^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error[E0277]: `T` doesn't implement `std::fmt::Debug`
--> src/main.rs:67:29
|
67 | println!("{:?}",_value);
| ^^^^^^ `T` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
Abstract types like T are forbidden from supporting any operations that aren't explicitly enabled for them. You need to add where T: Debug to your generic code to limit it to only types that can be printed.
Unfortunately, this is going to be annoying for "printf debugging". If you just want to print raw pointer value, then printf!("{}", ptr as usize) might work.
For Debug types there's {:p} format pattern that prints their address.
It looks like the forum software is eating things in <angle brackets>, probably because it thinks they’re html tags. Can you edit your post to put the code in a code block? You just need to put three backticks around it, like this:
Thanks. The simple fix is to add a Debug requirement for T. This slightly limits what buffer items you can use, but not by much; all the primitive types implement it, as does most of the standard library:
impl<T:Debug> CircBuf<T> {
/* your existing code */
}
I have changed the code but still getting errors :
error[E0277]: the trait bound `T: std::fmt::Pointer` is not satisfied
--> src/main.rs:67:29
|
67 | println!("{:p}",_value );
| ^^^^^^ the trait `std::fmt::Pointer` is not implemented for `T`
|
= note: required by `std::fmt::Pointer::fmt`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound
|
16 | impl<T:Debug + std::fmt::Pointer> CircBuf<T> {
| ^^^^^^^^^^^^^^^^^^^
error[E0599]: no method named `clear` found for mutable reference `&mut CircBuf<T>` in the current scope
--> src/main.rs:112:10
|
112 | self.clear();
| ^^^^^ method not found in `&mut CircBuf<T>`
|
= note: the method `clear` exists but the following trait bounds were not satisfied:
`T: std::fmt::Debug`
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `circ_buff`.
To learn more, run the command again with --verbose.
_value here is a T (char), so it doesn’t have any kind of pointer value — using {:p} here doesn’t make much sense, which is what the compiler is complaining about. What is the output that you’re expecting?
Sorry; I didn’t notice the Drop implementation. Copying the clear code there is the fastest way forward: what the previous change did is undefine all of those methods when T isn’t Debug. Since Drop doesn’t have that constraint, it can’t assume that clear is implemented.
I also just moticed that you’re mot calling any destructors, so you might want to limit T to Copy + Debug instead.
impl<T> Drop for CircBuf <T> {
fn drop (&mut self) {
self.tail = 0;
self.head = 0;
unsafe { alloc::alloc::dealloc(self.buffer as *mut u8, self.layout) };
}
}
error[E0277]: T doesn't implement std::fmt::Debug
--> src/main.rs:66:29
|
66 | println!("{:?}",_value);
| ^^^^^^ T cannot be formatted using {:?} because it doesn't implement std::fmt::Debug
|
= help: the trait std::fmt::Debug is not implemented for T
= note: required by std::fmt::Debug::fmt
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter T
|
15 | impl<T: std::fmt::Debug> CircBuf {
| ^^^^^^^^^^^^^^^^^
error: aborting due to previous error; 1 warning emitted
For more information about this error, try rustc --explain E0277.
error: could not compile circ_buff.
To learn more, run the command again with --verbose.