How do I use attributes on fields on a custom derive macro? [closed] - struct

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I'm writing my derive macro:
use proc_macro::{self, TokenStream};
#[proc_macro_derive(MyMacro, attributes(my_attr))]
pub fn my_macro(input: TokenStream) -> TokenStream {
...
TokenStream::from(my_generated_code)
}
with the attributes which will be used like:
use my_crate::MyMacro;
#[derive(MyMacro)]
#[my_attr(foo)]
struct MyStruct {
// some fields
#[my_attr(abc)]
a: i32,
}
But this snip leads to:
error: cannot find attribute `my_attr` in this scope
--> src/main.rs:10:7
|
6 | #[my_attr(abc)]
| ^^^^^^^
This is insanely weird because the attribute on struct is working well. Also I tried to change struct on enum and then field annotation has worked for some reason
May it be the problem of rustc, proc_macro version? rustc version: rustc 1.61.0-nightly (c5cf08d37 2022-03-30)
Macro-crate's Cargo.toml:
[lib]
proc-macro = true
[dependencies]
syn = {version= "1.0", features = ["full", "extra-traits"]}
quote = "1.0"
I found the same problem: issue, but the solution isn't clear at all and it's solved so I can't ask for some explanations

Related

To print simple struct or enum, derived from Debug or explicit fmt::Display implementation? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
I have a simple enum or struct and want to print. We know that being derived from Debug automatically enables a quick printing without explicit an implementation of fmt::Display. But you can still implement Display for printing it.
#[derive(Clone, Copy, PartialEq)]
pub enum MyDataType {
INVALID = 0,
TYPE1 = 1,
TYPE2 = 2,
}
let my_type: MyDataType = ...;
// This won't compile.
println!("{my_type}");
|
10 | println!("{my_type}");
| ^^^^^^^ `MyDataType` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `MyDataType`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
So, there're two approaches to make it work:
// Simply using Debug
#[derive(Debug)]
// Or, have your own Display implementation
impl fmt::Display for MyDataType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MyDataType::INVALID => write!(f, "INVALID"),
MyDataType::TYPE1 => write!(f, "TYPE1"),
MyDataType::TYPE2 => write!(f, "TYPE2"),
}
}
}
What are the tradeoffs between these two approaches? Which one do you prefer?
Your question seems to imply there's only two options, derive Debug or manually implement Display, which is not the case at all, you can do both (or neither) and for different reasons. Debug and Display are different traits for different purposes; Debug is developer-centric while Display is user-centric.
If the derived implementation is good enough for developers, great! You can use that! If not, you can implement it manually. And if that is also good enough for users, great! You can defer to the Debug implementation when implementing Display as #cdhowie suggested in the comments. If not, you can implement that manually as well.
I don't see any "tradeoffs" to really consider here. If the derived implementation isn't what you want, then don't use it. Its up to you what you need and how it should be formatted.

What's the proper way to get a string slice from a Rc<RefCell<String>>? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
use std::{cell::RefCell, rc::Rc};
fn main() {
let wrapped_string = Rc::from(RefCell::from(String::from("hello there my majesty")));
let partial: str = wrapped_string.borrow()[7..18];
}
gives the error:
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/main.rs:5:9
|
5 | let partial: str = wrapped_string.borrow()[7..18];
| ^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: all local variables must have a statically known size
= help: unsized locals are gated as an unstable feature
help: consider borrowing here
|
5 | let partial: str = &wrapped_string.borrow()[7..18];
| ^
Change
let partial: str = wrapped_string.borrow()[7..18];
to
let partial: &str = &wrapped_string.borrow()[7..18];

RUST compilation error: Expecting `::` instead got `{` [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am new to rust, and I am trying to compile my code with this function:
fn recieve(&mut self, timeout: u64) -> <Vec<Vec<u8>>> {
let mut bufs: Vec<Vec<u8>> = self.streams.iter().map(|_| Vec::new()).collect();
task::block_on(future::join_all(self.streams.iter_mut().zip(bufs.iter_mut()).map(|(s, b)| {
io::timeout(Duration::from_secs(timeout), s.read_to_end(b))
})));
Ok(bufs)
}
I get this error:
error: expected `::`, found `{`
--> src/lib.rs:60:59
|
60 | fn recieve(&mut self, timeout: u64) -> <Vec<Vec<u8>>> {
| ^ expected `::`
What is the issue? Can someone point me in the right direction?
Lose the angle-brackets surrounding the return type, and you should get past this problem.
Angle-brackets are only used in types to surround the argument provided to a generic type - they cannot surround any type like parentheses can surround any expression.

Cant Set Struct Property to Enum Rust [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a structure textApp with field mode, which has a type of a Mode:
pub struct textApp{
mode: Mode,
}
pub enum Mode {
Single,
Multiple,
}
I initialize a new instance of the textApp structure as myTextApp, and I have a button then when clicked I want it to change the value of myTextApp's .mode field to Mode::Single:
fn main(){
let mut myTextApp = textApp{
mode: Mode::Multiple,
};
singleModeBut.set_callback(move||{
let newMode: Mode = Mode::Single;
textApp.mode = newMode;
//throws an error
});
}
but this gives me the error:
error[E0423]: expected value, found struct `textApp`
| textApp.mode = newModeSet;
| ^^^^^^^-----
| |
| help: use the path separator to refer to an item: `textApp::mode`
Why can't I set this struct field to Mode::Single?
Your struct's instance name is myTextApp. You are trying to set using struct's name which doesn't make sense.
Changing,
textApp.mode = newMode;
to
myTextApp.mode = newMode;
will fix it.
Playground
Also, it's a good idea to follow rust style guidelines that ask you to use snake case for identifier names and upper camel case for type names. That will help you avoid such errors.

In which situations would Stdin::lock be useful? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Stdin::lock:
pub fn lock(&self) -> StdinLock<'_>
In which situations would this be useful?
The documentation states, emphasis mine:
A handle to the standard input stream of a process.
Each handle is a shared reference to a global buffer of input data to this process. A handle can be lock'd to gain full access to BufRead methods (e.g., .lines()). Reads to this handle are otherwise locked with respect to other reads.
use std::io::{self, prelude::*};
fn main() {
let stdin = io::stdin();
dbg!(stdin.lines().count()); // fails!
let stdin = stdin.lock();
dbg!(stdin.lines().count());
}
error[E0599]: no method named `lines` found for struct `std::io::Stdin` in the current scope
--> src/main.rs:6:16
|
6 | dbg!(stdin.lines().count());
| ^^^^^ method not found in `std::io::Stdin`
|
= note: the method `lines` exists but the following trait bounds were not satisfied:
`std::io::Stdin: std::io::BufRead`
which is required by `&mut std::io::Stdin: std::io::BufRead`

Resources