I couldn't find a working example for Rust-SIMD. The closest I can find is this one. After adjusting, it becomes:
#![feature(core)]
#![feature(portable_simd)]
use std::simd::f32x4;
fn main() {
let x = f32x4(1.0, 1.0, 1.0, 1.0);
}
But still cargo complains
error[E0423]: expected function, found type alias `f32x4`
--> src/main.rs:7:13
|
7 | let x = f32x4(1.0, 1.0, 1.0, 1.0);
| ^^^^^
|
= note: can't use a type alias as a constructor
during building.
How do I get this simple example working?
Cargo.toml:
[package]
name = "rust-simd"
version = "0.1.0"
edition = "2021"
[dependencies]
I have nightly switched on already: rustup default nightly.
The way to construct a SIMD vector with std::simd is type::from(array), for example f32x4::from([1.0, 1.0, 1.0, 1.0]). This is mentioned in the documentation.
Related
I am trying to draw an image on a very basic GUI. I am using GTK for the GUI and want to use the plotters crate for the drawing but am having issues initiating the Backend that plotters uses.
I have successfully managed to use the BitMapBackend to draw to a file - there is no shortage of examples on the internet for this. However, I cannot for the life of me find an example of how to use the CairoBackend to draw onto a gtk DrawingArea.
My issue arises when I try to connect the draw event to the GTK DrawingArea. I use the connect_draw method which works as expected returning a context to pass to the CairoBackEnd. The problem is that the type of context returned by connect_draw is not compatible with the type of context required by the CairoBackend. I receive context of type 'gtk::cairo::Context' (defined in cairo-rs v0.16.7) but the backend requires a context of 'cairo::context:Context' (defined in cairo-rs v0.15.12). Clearly my dependencies are using different versions of the cairo-rs crate. Presumably, the versions of my dependencies are not compatible with each other. How can I fix this? How do I find out what version of each dependency will work with the others? My cargo.toml file contains:
[package]
name = "testing3"
version = "0.1.0"
edition = "2021"
[dependencies]
gtk = "0.16.2"
plotters = "0.3.4"
plotters-cairo = "0.3.2"
My rust code is:
use gtk::prelude::*;
use plotters::prelude::*;
use gtk::{Application, ApplicationWindow};
fn main() {
let app = Application::builder().build();
app.connect_activate(build_gui);
app.run();
}
fn build_gui(app: &Application){
let win = ApplicationWindow::builder().application(app).default_width(320).default_height(200).title("Test").build();
let da: gtk::DrawingArea = gtk::DrawingArea::builder().build();
da.connect_draw(move|_, c|{
let root = plotters_cairo::CairoBackend::new(c, (1024, 768)).unwrap().into_drawing_area();
gtk::Inhibit(false)
});
win.add(&da);
win.show_all();
}
my error message is:
--> src/main.rs:21:49
|
21 | let root = plotters_cairo::CairoBackend::new(c, (1024, 768)).unwrap().into_drawing_area();
| --------------------------------- ^ expected struct `cairo::context::Context`, found struct `gtk::cairo::Context`
| |
| arguments to this function are incorrect
|
= note: struct `gtk::cairo::Context` and struct `cairo::context::Context` have similar names, but are actually distinct types
note: struct `gtk::cairo::Context` is defined in crate `cairo`
--> /home/maindesktop/.cargo/registry/src/github.com-1ecc6299db9ec823/cairo-rs-0.16.7/src/context.rs:72:1
|
72 | pub struct Context(ptr::NonNull<cairo_t>);
| ^^^^^^^^^^^^^^^^^^
note: struct `cairo::context::Context` is defined in crate `cairo`
--> /home/maindesktop/.cargo/registry/src/github.com-1ecc6299db9ec823/cairo-rs-0.15.12/src/context.rs:72:1
|
72 | pub struct Context(ptr::NonNull<cairo_t>);
| ^^^^^^^^^^^^^^^^^^
= note: perhaps two different versions of crate `cairo` are being used?
note: associated function defined here
--> /home/maindesktop/.cargo/registry/src/github.com-1ecc6299db9ec823/plotters-cairo-0.3.2/src/backend.rs:70:12
|
70 | pub fn new(context: &'a CairoContext, (w, h): (u32, u32)) -> Result<Self, CairoError> {
| ^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `testing3` due to previous error
------------------
(program exited with code: 101)
So with more digging around I have found the solution.
As I identified from the error message, my code had a compatibility problem. The gtk v0.16.2 dependency in my toml file depended itself on cairo-rs v0.16.7 where as the plotters-cairo v0.3.2 depended on cairo-rs v15.12. Clearly, I had to use an earlier version of the gtk dependency - one that depended on cairo-rs v15.12. But which one.
On the Doc.rs website I could find the gtk crate documents on https://docs.rs/gtk/0.6.0/gtk/. A tab on top left, gtk-0.6.0, on being selected provided both dependency data and versioning options. I could see that the latest version of the gtk crate did indeed depend on cairo-rs 0.6.0. Selecting the various versions I was able to find the latest version that still depended on cairo-rs v15.xx. In this case it was gtk v0.15.5. Once I replaced my gtk dependency with that version, all was good. So my new toml was:
[package]
name = "testing3"
version = "0.1.0"
edition = "2021"
[dependencies]
gtk = "0.15.5"
plotters = "0.3.4"
plotters-cairo = "0.3.2"
Now by adding the code:
c.rectangle(10.0, 10.0, 70.0, 70.0);
c.fill().expect("Drawing failed");
root.fill(&RED).unwrap();
above my gtk::Inhibit(false) statement, I could indeed create a red rectangle
I'm trying out the "Using Derive Macros" example on the index page for the latest beta version of clap:
// (Full example with detailed comments in examples/01d_quick_example.rs)
//
// This example demonstrates clap's full 'custom derive' style of creating arguments which is the
// simplest method of use, but sacrifices some flexibility.
use clap::{AppSettings, Parser};
/// This doc string acts as a help message when the user runs '--help'
/// as do all doc strings on fields
#[derive(Parser)]
#[clap(version = "1.0", author = "Kevin K. <kbknapp#gmail.com>")]
struct Opts {
/// Sets a custom config file. Could have been an Option<T> with no default too
#[clap(short, long, default_value = "default.conf")]
config: String,
/// Some input. Because this isn't an Option<T> it's required to be used
input: String,
/// A level of verbosity, and can be used multiple times
#[clap(short, long, parse(from_occurrences))]
verbose: i32,
#[clap(subcommand)]
subcmd: SubCommand,
}
...
Unfortunately it fails to compile:
$ cargo build
Compiling ex v1.0.0-SNAPSHOT (/home/hwalters/git/home/ex)
error: cannot find derive macro `Parser` in this scope
--> src/main.rs:5:10
|
5 | #[derive(Parser)]
| ^^^^^^
|
note: `Parser` is imported here, but it is only a trait, without a derive macro
--> src/main.rs:1:25
|
1 | use clap::{AppSettings, Parser};
| ^^^^^^
error: cannot find attribute `clap` in this scope
--> src/main.rs:6:3
|
6 | #[clap(version = "1.0", author = "Kevin K. <kbknapp#gmail.com>")]
| ^^^^
|
= note: `clap` is in scope, but it is a crate, not an attribute
...
I tried to find the full example file "examples/01d_quick_example.rs" in the tar file for this GitHub tag, but it does not seem exist there.
I appreciate this is a beta version, but is this functionality expected to work, or am I doing something wrong?
Thanks!
In clap, use features = [ "derive" ] in Cargo.toml to enable the ability to derive :)
update
#stein below makes a good point of expanding the answer:
To "use" means: in the [dependencies] section, specify clap with a
line similar to: clap = { version = "3.1.0", features = ["derive"]} –
Stein Feb 19 at 13:00
Please +1 their comment :-).
This means, in your Cargo.toml
[dependencies]
# ...
clap = { version = "3", features = ["derive"]}
Recently I knew about web assembly and was messing around with it in rust. Eventually I run into build problem. My toml file looks like this
[package]
name = "clipboard-manager"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["console_error_panic_hook"]
[dependencies]
x11-clipboard = "0.5.2"
wasm-bindgen = "0.2.63"
js-sys = "0.3.55"
console_error_panic_hook = { version = "0.1.6", optional = true }
#libc = "0.2.105"
# log = "0.4.14"
# x11 = "2.19.1"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
[lib]
crate-type = ["cdylib", "rlib"]
And when I want to build .wasm file with wasm-pack build --target web command I get bunch of errors like this
error[E0425]: cannot find function `malloc` in crate `libc`
--> /mnt/commondisk/filip/Development/personal/clipboard-manager/target/wasm32-unknown-unknown/release/build/xcb-64883900109c6e35/out/shape.rs:98:29
|
98 | let raw = libc::malloc(32 as usize) as *mut xcb_shape_notify_event_t;
| ^^^^^^ not found in `libc`
error[E0425]: cannot find function `malloc` in crate `libc`
--> /mnt/commondisk/filip/Development/personal/clipboard-manager/target/wasm32-unknown-unknown/release/build/xcb-64883900109c6e35/out/xfixes.rs:238:29
|
238 | let raw = libc::malloc(32 as usize) as *mut xcb_xfixes_selection_notify_event_t;
| ^^^^^^ not found in `libc`
error[E0425]: cannot find function `malloc` in crate `libc`
--> /mnt/commondisk/filip/Development/personal/clipboard-manager/target/wasm32-unknown-unknown/release/build/xcb-64883900109c6e35/out/xfixes.rs:330:29
|
330 | let raw = libc::malloc(32 as usize) as *mut xcb_xfixes_cursor_notify_event_t;
| ^^^^^^ not found in `libc`
I maneged to build wasm module by commenting out x11-clipboard dependency. I tried to build this module with x11-clipboard dependencies and it was also failing until I commented out xcb dependency. It also fails if I add x11 crate. For a while I thought libc could be a problem but I had not problem building module with this dependency.
Does anyone know why some dependencies cause building process failing and how to fix it? If it's impossible to fix can someone explain me why? Thanks in advance!
I'm trying to create a Vector3 from the nalgebra crate with components (1.0, 1.0, 1.0). I can't find what I need from the documentation, and my IDE's suggestions don't help.
To be fair, the types in nalgebra are a little complicated: Vector3<N> is a type alias of VectorN<N, U3>, which is a type alias of MatrixMN<N, D, U1>, which is a type alias of Matrix<N, R, C, Owned<N, R, C>>!
The documentation for Matrix includes a large number of different new methods, depending on the constraints, and you want this one.
let my_vec = Vector3::new(1.0, 1.0, 1.0);
I can do this:
let a: [f32; 3] = [0.0, 1.0, 2.0];
But why doesn't this work?
let a: [f32; _] = [0.0, 1.0, 2.0];
It seems to me that the length is redundant and trivial to infer. Is there a way to avoid having to specify it explicitly? (And without having to append f32 to all the literals.)
_ can only be used in two contexts: in patterns, to match a value to ignore, and as a placeholder for a type. In array types, the length is not a type, but an expression, and _ cannot be used in expressions.
What you can do, though, is append f32 to only one of the literals and omit the type completely. Since all the items of an array must have the same type, the compiler will infer the correct element type for the array.
let a = [0.0f32, 1.0, 2.0];
Since 1.39 it's possible using a simple macro
macro_rules! arr {
($id: ident $name: ident: [$ty: ty; _] = $value: expr) => {
$id $name: [$ty; $value.len()] = $value;
}
}
Usage
arr!(static BYTES: [u8; _] = *b"foo");
arr!(let floats: [f32; _] = [0., 1.]);
It is now possible on nightly with the generic_arg_infer feature, for both the type and the initializer:
#![feature(generic_arg_infer)]
let _arr: [f32; _] = [0.0, 1.0, 2.0];
let _arr: [f32; 3] = [0.0; _];
If you are dealing with constants or static variables (and you have to specify the type) it's common to use a slice type instead:
static FOO: &[f32] = &[0.0, 1.0, 2.0];
This was originally a comment by Lukas Kalbertodt to another answer.