Where is checksumValid() defined? - rust

I am working from a compliance perspective. I am struggling with the simplest thing, how to find the source for various functions that are called by rustup.
For example, the rustup source code references checksumValid, but I can't find it defined anywhere. I searched stackoverflow, I searched Google, etc.
I'd appreciate help finding checksumValid, but more importantly, what is wrong with my approach?
pub enum Notification<'a> {
...
NoUpdateHash(&'a Path),
ChecksumValid(&'a str),
SignatureValid(&'a str, &'a PgpPublicKey),
...

ChecksumValid(&'a str) here is not actually a function but rather one of many possible values of an enum Notification defined in this line. While the () is usually associated with functions in rust it can denote other things as well. In this case it denotes one possible value of the enum that contains some data that is of type &str with a lifetime of 'a. This is the definition of this value, in this line right here.
You might want to read this to understand the syntax.

Related

How to make sure that a Rust attribute proc macro runs before the compiler tries parsing the item?

I'm trying to make a macro that adds some extra syntax to struct type declarations.
For simplicity, here's a toy example: a macro that replaces "function call"-style type declarations with normal ones.
#[my_macro]
struct Point {
x: LiteralType("f32"),
y: LiteralType("f32"),
}
should turn into
struct Point {
x: f32,
y: f32,
}
From the output of my code and cargo expand, I can tell that my code produces the correct syntax.
However, before that happens, the compiler has already tried parsing the LiteralType("f32") syntax, failed and emitted an error saying as much, and now the whole build fails.
I think I need to somehow force the compiler to run my macro's code to completion before trying to parse it according to the regular Rust rules. I think it might be possible to do using functional macros, because those can accept syntax that's much more unusual, but I'd prefer to stick with attribute macros because those provide a secondary input for attributes.
You cannot do that with attribute macros, they always validate their items.
But as you said, you can do that with function-like macros.

Is there a pattern to change compile behavior based on a type (proc macros)

I am trying to write a procedural macro that performs different behavior based on a trait type, which the macro may not have access to. For example, given the following trait:
trait Bar {
type BarType;
}
I would like to be able to change macro behavior based on the chosen BarType, even when my macro may not have direct access to it:
// This macro sees nothing about BarType
#[macro_option_1]
struct Foo {}
// this macro sees nothing about BarType
macro_option_2!(Foo);
// This location works but is not desirable
// #[working_macro_option]
impl Bar for Foo {
type BarType = Option<u32>;
}
A macro at working_macro_option would have access to BarType, but this location is not desirable (as my actual macro needs to work with multiple traits). So, my goal is to be able to use a macro that works at either macro_option_1 or macro_option_2, which have no useful content available to parse.
I am hoping for a pattern along the lines of what quote_spanned makes possible (link to example), but changing what the macro does in some way. So, I believe this needs to be something that's directly compilable, rather than something possible only in proc macros.
(this is unfortunately outside the scope of generics - my macro needs to create a function with a completely different signature based on type, for a C FFI)
Edit: more context on my end goal.
I am writing a library that helps users create a UDF, which is a dylib with a C interface. The init, process, add, remove etc. functions map nicely to traits (see my unfinished work here). My goal is that a user of my library can:
Define a struct or enum (which may be zero-sized, or contain something useful) which will give their UDF name
Implement my traits on this struct (one trait is required, the second is optional)
Add a #[register] macro to this struct
And from there, my library will generate the correct C interface functions via the macro. This isn't too different from how PyO3 works, I believe.
However, some of the C function signatures depend on the Return type within BasicUdf, so my macro needs some way to know what this is. I find it cleaner to put the macro on the main struct, so I am looking for a way to do this (hence this question)
My backup plan is to require placing the macro on the impl, which isn't terrible but is just less elegent (parsing the Returns type there is no problem). Something like a PhantomData marker on the main struct would also work, but I don't find that too ergonomic.

In Rust, what kind of syntax is ::<Template>? [duplicate]

I read the below syntax from byteorder:
rdr.read_u16::<BigEndian>()
I can't find any documentation which explains the syntax instance.method::<SomeThing>()
This construct is called turbofish. If you search for this statement, you will discover its definition and its usage.
Although the first edition of The Rust Programming Language is outdated, I feel that this particular section is better than in the second book.
Quoting the second edition:
path::<...>, method::<...>
Specifies parameters to generic type, function, or method in an expression; often referred to as turbofish (e.g., "42".parse::<i32>())
You can use it in any kind of situation where the compiler is not able to deduce the type parameter, e.g.
fn main () {
let a = (0..255).sum();
let b = (0..255).sum::<u32>();
let c: u32 = (0..255).sum();
}
a does not work because it cannot deduce the variable type.
b does work because we specify the type parameter directly with the turbofish syntax.
c does work because we specify the type of c directly.

No method write found in TcpStream in rust [duplicate]

I tried to compile the following program:
use std::io;
fn main() {
io::stdout().write(b"Please enter your name: ");
io::stdout().flush();
}
Unfortunately, compiler resisted:
error: no method named `write` found for type `std::io::Stdout` in the current scope
--> hello.rs:4:18
|
4 | io::stdout().write(b"Please enter your name: ");
| ^^^^^
|
= help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
= help: candidate #1: `use std::io::Write`
I found that I needed to do use std::io::{self, Write};. What does use std::io; actually do then and how do (if possible) I pull all names defined in std::io? Also, would it be a bad style?
What does use std::io; actually do then?
It does what every use-statement does: makes the last part of the used path directly available (pulling it into the current namespace). That means that you can write io and the compiler knows that you mean std::io.
How do I pull all names defined in std::io?
With use std::io::*;. This is commonly referred to as glob-import.
Also, would it be a bad style?
Yes, it would. Usually you should avoid glob-imports. They can be handy for certain situations, but cause a lot of trouble on most cases. For example, there is also the trait std::fmt::Write... so importing everything from fmt and io would possibly result in name-clashes. Rust values explicitness over implicitness, so rather avoid glob-imports.
However, there is one type of module that is usually used with a glob-import: preludes. And in fact, there is even a std::io::prelude which reexports important symbols. See the documentation for more information.

Why is String::find not a method on &str?

I recently noticed that String::find is actually a method on an owned String.
But I can't see why it wouldn't just be a method on &str instead, making it useful in more cases (and still being just as useful for String). Am I missing a reason for why it's like this, or is it just a historical accident?
Apparently, the documentation confused you. This method is listed under this section:
So it is not even implemented for String, but indeed just for &str.
Actually it's only available for String because it Derefs to str:
Methods from Deref<Target=str>
You won't find it in the source for String, but in the source for str.
Actually... you are wrong: it is not a String method.
What you are looking at is str::find.
It just so happens that the Rust documentation automatically includes on the String page the methods brought in by the fact that String implements Deref<Target=str> as can be seen here.
Why does the documentation includes the methods that can be called on the target of Deref?
Because you can actually call them directly on a String object, since the compiler will automatically follow Deref if it does not find the method you are calling, recursively.

Resources