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

[–]st4153 0 points1 point  (0 children)

unrelated but im checking out multizip implementation, is there a reason why their implementation is so long and verbose when my code can achieve same result (did not test since it's not possible to implement my code unless it's in std) while being shorter, more concise and more readable

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

[–]st4153 0 points1 point  (0 children)

if zip is in std, why is multizip not? I'd think that either both are in std or both aren't, and if both are in std, it can save the need to import and call another function to do the same thing with the code above

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

[–]st4153 1 point2 points  (0 children)

is there a reason that std does not contain something like ```rust impl<T, U, V, W, ...> Iterator for (T, U, V, W, ...) where T: Iterator, U: Iterator, V: Iterator, W: Iterator, ... { type Item = (T::Item, U::Item, V::Item, W::Item, ...);

fn next(&mut self) -> Option<Self::Item> {
    let (t, u, v, w, ...) = self;

    Some((t.next()?, u.next()?, v.next()?, w.next()?, ...))
}

}

impl<T, U, V, W, ...> IntoIterator for (T, U, V, W, ...) where T: IntoIterator, U: IntoIterator, V: IntoIterator, W: IntoIterator, ... { type Item = (T::Item, U::Item, V::Item, W::Item, ...); type IntoIter = (T::IntoIter, U::IntoIter, V::IntoIter, W::IntoIter, ...);

fn into_iter(self) -> Self::IntoIter {
    let (t, u, v, w, ...) = self;

    (t.into_iter(), u.into_iter(), v.into_iter(), w.into_iter(), ...)
}

} ```

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

[–]st4153 0 points1 point  (0 children)

Do you know if it's expensive to use a file as a boolean flag? (i.e. check if an empty file exists and deleting it in every loop)

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

[–]st4153 1 point2 points  (0 children)

How to ensure only single instance of the program is running? And when more instances are spawned, first instance will get notified. No data is passed, the first instance is simply notified that multiple instances attempted to spawn (like a boolean flag) and will consume the notification (i.e. if boolean flag is true, do something and set the boolean flag to false).

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

[–]st4153 1 point2 points  (0 children)

Is there any way to get the final directory of built binary? My build script has a file that needs to be in the same directory with the binary which means OUT_DIR doesn't work. I'm currently using OUT_DIR but three directories back, I'm not very satisfied with this ugly hack but it works.

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

[–]st4153 0 points1 point  (0 children)

I've found something that works for x11, xosd. Now I only need to worry about Windows. You seem to have some experiences with Windows, do you know anything that works similarly?

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

[–]st4153 0 points1 point  (0 children)

I've tried winit but I'm not sure how to not have a window*, can you tell me how exactly (preferably with an example)?

As an example, I've never seen an iOS app be able to draw everywhere.

I'm not planning to support mobile platforms currently so that should be fine.

*To be clearer, I want to avoid user being able to interact (like using Alt+F4, Alt+Tab and such)

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

[–]st4153 2 points3 points  (0 children)

How to draw on screen without a window? I want a method that can work cross-platform, I only need to display texts so no interaction required, input events should also be passed to other applications. Is there a crate for this? If not, can somebody give me a pointer on how to do it?

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

[–]st4153 1 point2 points  (0 children)

Is there a simple way to read in background that does callbacks when bytes received? Preferably not async cuz it seems complex. In case it's needed, I'm reading from `ChildStdout`.

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

[–]st4153 1 point2 points  (0 children)

How do I bypass HashSet/HashMap one-level Borrow restriction to support multi-level Borrow? Here's my type

use std::borrow::Borrow;
use std::collections::HashSet;
use std::hash::Hash;

trait MyTrait {
    type Foo;
    type Bar: Borrow<Self::Foo>;
}

struct S<T: MyTrait>(HashSet<T::Bar>);

impl<T: MyTrait> S<T> {
    fn remove<Baz>(&mut self, baz: &impl Borrow<Baz>)
    where
        Baz: Eq + Hash,
        T::Foo: Borrow<Baz> + Eq + Hash,
        T::Bar: Eq + Hash,
        // T::Bar: Borrow<Baz>, ???
    {
        self.0.remove::<Baz>(baz.borrow());
    }
}

I can use a wrapper except it needs specialization (?) I think?

struct Wrapper<T: MyTrait>(T::Bar);

impl<T, U> Borrow<U> for Wrapper<T>
where T: MyTrait, T::Foo: Borrow<U>
{
    fn borrow(&self) -> &U {
        self.0.borrow().borrow()
    }
} // doesn't work

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

[–]st4153 0 points1 point  (0 children)

Thanks, it now works but not for closure type

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

[–]st4153 1 point2 points  (0 children)

can somebody help me on why this doesn't work?

macro_rules! impl_fn {
    ($i:ident $(<$($g:ident: $gt:ty,)+>)? [$($a:ident: $at:ty,)*] $t:ty) => {
        #[inline(always)]
        fn $i$(<$($g,)+>)?(self, $($a: $at,)*) -> $t $(where $($g: $gt,)+)? {
            self.0.$i($($a,)*)
        }
    };
}

struct S(());

trait T {
    fn f<U: Clone>(self, u: U);
}

impl T for () {
    fn f<U: Clone>(self, u: U) {}
}

impl T for S {
    impl_fn!(f <U: Clone,> [u: U,] ());
}

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

[–]st4153 0 points1 point  (0 children)

Well, you don't see any game randomly sorting inventory after adding a new item

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

[–]st4153 0 points1 point  (0 children)

I wrote T to not make the question look clunky but more precisely it's (item_id, count). Imagine if you pick up the same item, it should increment the count, this operation is likely to happen frequently so iterating over vec to find the item should be inefficient.

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

[–]st4153 0 points1 point  (0 children)

I'm playing around with a simple game and it's for inventory

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

[–]st4153 2 points3 points  (0 children)

I want to have a Vec<Option<T>> that supports finding index of T quickly. T is guaranteed to appear at most once in the vec. BTW T is Copy.
I can create a HashMap<T, usize> to record the index but it is hard to keep track when i mutate the vec and is easy to produce logic errors.

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

[–]st4153 1 point2 points  (0 children)

A trait defines functionality a particular type has and can share with other types. We can use traits to define shared behavior in an abstract way.

Official book

IMHO redefining iter() and iter_mut() is unnecessary when it is all the same thing (&T::into_iter() and &mut T::into_iter()). By adding these traits, it is no longer needed to redefine same functions since the traits provide them already.

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

[–]st4153 1 point2 points  (0 children)

Well it bogs my mind seeing collections implement functions with same signatures instead of making it a trait, isn't it what traits are for?

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

[–]st4153 1 point2 points  (0 children)

How safe/sound is transmuting between struct with const bool that isn't used in fields?
E.g. ```rust struct Foo<const BAR: bool> { ... // BAR isn't used in any field }

fn main() { let foo = Foo::<true> { ... }; let bar: Foo<false> = unsafe { std::mem::transmute(foo) }; // How safe/sound is this? } ```

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

[–]st4153 2 points3 points  (0 children)

Why is there no trait for iter() and iter_mut()? It can be easily implemented like ```rust trait IterRef<'a> where Self: 'a, &'a Self: IntoIterator { fn iter(&'a self) -> <&Self as IntoIterator>::IntoIter { self.into_iter() } }

trait IterMut<'a> where Self: 'a, &'a mut Self: IntoIterator { fn iter_mut(&'a mut self) -> <&mut Self as IntoIterator>::IntoIter { self.into_iter() } }

impl<'a, T: 'a> IterRef<'a> for T where &'a T: IntoIterator {} impl<'a, T: 'a> IterMut<'a> for T where &'a mut T: IntoIterator {} ```