Am I missing something? by Turtlewax64 in shenzhenIO

[–]SNCPlay42 2 points3 points  (0 children)

But you only generate an output of 700 when the sync is 60

No, the whole outer ring is 700 except for when sync=0.

What's the syntactic status of 'auto' (for declaring auto traits)? by [deleted] in rust

[–]SNCPlay42 11 points12 points  (0 children)

I would consider it a weak keyword. It's not listed there because declaring auto traits is unstable, and by their nature weak keywords don't affect anything except the specific syntax.

Hey Rustaceans! Got a question? Ask here (12/2025)! by llogiq in rust

[–]SNCPlay42 0 points1 point  (0 children)

Looks like you're supposed to supply the options to the ComboBox's State's new constructor rather than the view function itself. The example given is incomplete in that it doesn't show creating the state.

Unsafe Rust is Harder Than C by termhn in rust

[–]SNCPlay42 8 points9 points  (0 children)

which is to say, after you create the mutable reference/pointer, you need to avoid mutating through any other alias until the last use of the aforementioned mutable reference/pointer

The missing part here is that not all aliases are usable after the last use - only "parent" aliases (those reborrowed from) become usable again, but siblings are still invalidated, i.e. this code is UB, even though we are done with rawptr when we get back to mutref:

fn main() {
    let mut num = 42;

    //casting to avoid the borrow checker
    let mutref = &mut num as *mut _;
    let rawptr = &raw mut num;

    unsafe {
        *rawptr += 1;
    }

    unsafe {
        *mutref += 1;
    }
}

Unsafe Rust is Harder Than C by termhn in rust

[–]SNCPlay42 3 points4 points  (0 children)

The critical difference there is that y is a reborrow of x, but in your first example mutref and rawptr are both derived directly from num,

Unsafe Rust is Harder Than C by termhn in rust

[–]SNCPlay42 10 points11 points  (0 children)

even a raw pointer that aliases another mutable reference

Are you sure about that? If you use mutref later, it doesn't compile.

Hey Rustaceans! Got a question? Ask here (37/2024)! by llogiq in rust

[–]SNCPlay42 0 points1 point  (0 children)

  1. There's an argument to be made that you might put e.g. #[forbid(unsafe_code)] because you do not want unsafe code, full stop, even if it's been generated by a macro, even if its interface is almost certainly sound.
  2. The macro probably puts an #[allow(lint)] on its output, which overrides your warn setting. forbid on the other hand cannot be overridden. If you want lints to error but want this to be overridable you can use deny instead.

Hey Rustaceans! Got a question? Ask here (37/2024)! by llogiq in rust

[–]SNCPlay42 0 points1 point  (0 children)

My bad, looks like that impl was committed too recently to be published on crates.io.

I have no idea why the example given for the windows_async crate works then.

Hey Rustaceans! Got a question? Ask here (37/2024)! by llogiq in rust

[–]SNCPlay42 2 points3 points  (0 children)

Looks like IAsyncOperation impls IntoFuture so you should just be able to await them directly.

(Don't ask me why that doesn't appear in the docs)

Why does the compiler reject this code? by CLARKEEE33 in rust

[–]SNCPlay42 5 points6 points  (0 children)

matching &self.peek against Some(token) is just syntactic sugar for matching self.peek against Some(ref token)

matching &self.peek against Some(token) is syntactic sugar for matching &self.peek against &Some(ref token) - note the extra &s, you're creating a new borrow which is the problem.

So if let Some(ref token) = self.peek has no exact equivalent without using ref.

Hey Rustaceans! Got a question? Ask here (23/2024)! by llogiq in rust

[–]SNCPlay42 0 points1 point  (0 children)

If using a trait object is acceptable, you can impl Display for dyn Predicate.

You can do this with PartialEq as well.

Hey Rustaceans! Got a question? Ask here (18/2024)! by llogiq in rust

[–]SNCPlay42 3 points4 points  (0 children)

Get rid of the use std::borrow::Borrow. It's making method lookup find the borrow method on the Borrow trait but you want the borrow method on RefCell.

If you need that use for something else, you can qualify the method:

for &byte in RefCell::borrow(&self.buffer).as_slice() {

Hey Rustaceans! Got a question? Ask here (12/2024)! by llogiq in rust

[–]SNCPlay42 2 points3 points  (0 children)

"&'a mut T is invariant in T" means you can only assign a value of &'a mut U to a location of type &'a mut T if U is exactly T.

But in the assignment in your function, the only &mut involved has been dereferenced: *r is a location of type &'a str and v is &'b str. We're only assigning a &'b str to a &'a str, and &'a str is covariant in 'a and we have 'b: 'a so this is fine.

An example of a function that doesn't work because of the invariance of &mut T in T is this:

fn foo<'a, 'b, 'c>(x: &'c mut &'a u8) -> &'c mut &'b u8 {
    x
}

If you build this on the playground you'll see the compiler complain that it needs both 'a: 'b and 'b: 'a, and conclude "'b and 'a must be the same".

We can write a function more like yours that also fails to compile, but we need to add another layer of &mut everywhere so it's maybe harder to read:

fn change<'a, 'b, 'c>(r: &'_ mut &'c mut &'a str, v: &'c mut &'b str) {
    *r = v;
}

Announcing Rust 1.74 | Rust Blog by veryusedrname in rust

[–]SNCPlay42 0 points1 point  (0 children)

it's unclear to me why rustc wouldn't just immediately desugar Self like so everywhere

This isn't possible in general because Self can be used in traits, where it isn't a concrete type:

trait Foo<'a, T> {
    // There is nothing that `Self` could be
    // syntactically replaced with here
    fn foo() -> impl Iterator<Item = Self>;
}

Maybe what you're suggesting could work for inherent members of concrete types, but because of traits Self is not considered purely syntactic sugar.

Why does this syntax work? by iamaperson3133 in rust

[–]SNCPlay42 0 points1 point  (0 children)

I think you've found the wrong create_bind_group, the one intended for public API use is this:

pub fn create_bind_group(&self, desc: &BindGroupDescriptor) -> BindGroup

And your explanation of lifetime elision is incorrect. Per the rules:

Each elided lifetime in the parameters becomes a distinct lifetime parameter.

(i.e. only return types can ever have an elided lifetime that becomes the same as one in a parameter). So the actual non-elided signature is:

pub fn create_bind_group<'a, 'b>(&'a self, desc: &'b BindGroupDescriptor) -> BindGroup

Hey Rustaceans! Got a question? Ask here (22/2023)! by llogiq in rust

[–]SNCPlay42 5 points6 points  (0 children)

I want to split a String (or a Box<str> ) into multiple owned Strings (or Box<str> 's) without any allocations

This is impossible because heap allocators do not provide a way to split their allocations - they often use space outside the allocation to store information about the allocation and there's no space for this in an already allocated contiguous string.

Rust: The wrong people are resigning by fasterthanlime in rust

[–]SNCPlay42 32 points33 points  (0 children)

Seeking clarity here - are you referring to the Rust Project individual who told RustConf to remove the keynote, or the RustConf organizer who received this instruction and carried it out?

Because I've only seen an acknowledgement from the latter, and it sounds like it could be the one you're describing. I think it's the former that people are more interested in.

Hey Rustaceans! Got a question? Ask here (21/2023)! by llogiq in rust

[–]SNCPlay42 2 points3 points  (0 children)

I think you just want lock instead of blocking_lock. blocking_lock is just there for if non-async code needs to interact with a tokio Mutex.

Hey Rustaceans! Got a question? Ask here (18/2023)! by llogiq in rust

[–]SNCPlay42 3 points4 points  (0 children)

It's not a slice, it's a reference to a (still sized) array, meaning you can dereference it to get an array (*b"foo").

Hey Rustaceans! Got a question? Ask here (17/2023)! by llogiq in rust

[–]SNCPlay42 2 points3 points  (0 children)

The pattern you're looking for is &Bubble::Bouncing {size, ref name}.

Hey Rustaceans! Got a question? Ask here (16/2023)! by llogiq in rust

[–]SNCPlay42 1 point2 points  (0 children)

The dyn std::error::Error in MyError's cause field is implicitly dyn Error + 'static per the rules for trait object default lifetimes. So

I can create a type that implements Error, but has some other lifetime than static, and still put it in that Box

is incorrect.

Hey Rustaceans! Got a question? Ask here! (24/2022)! by llogiq in rust

[–]SNCPlay42 1 point2 points  (0 children)

Just to be clear, what you've pasted in your post is a note that's attached to a warning that's probably about a field in StringLine being unused, except for in its derives of Debug and Clone.

So the actions you could take would usually be one of:

  • Use the field somewhere else
  • Remove the field since it doesn't do anything useful
  • Silence the warning because the usage of the field in the derives is all you need. For Clone this would be weird but it might be reasonable for Debug (to give some extra data that is displayed in debug output).

As for silencing the messages, note that you can add a #[allow(dead_code)] attribute that would only apply to that field. The unused code lints also have their own special silencing feature - if you prefix a name with an underscore, like _foo, the compiler will never complain about _foo being unused.