kionite231

joined 2 years ago
MODERATOR OF
[–] kionite231 1 points 1 month ago (3 children)

yeah, you are right, I don't need mut. here is the improved version:

use axum::{extract::Path, routing::get, routing::post, Router};
use std::fs::{read_to_string, File};
use std::io::prelude::*;
use std::sync::atomic::{AtomicUsize, Ordering};

const MAX_FILE_SIZE: usize = 1024 * 1024 * 10;
static FILE_COUNT: AtomicUsize = AtomicUsize::new(0);

async fn handle(Path(id): Path<String>) -> String {
    match read_to_string(id) {
        Ok(content) => content,
        Err(e) => e.to_string(),
    }
}

async fn submit_handle(bytes: String) -> String {
    dbg!(&bytes);
    if bytes.len() > MAX_FILE_SIZE {
        // Don't store the file if it exceeds max size
        return String::from("ERROR: max size exceeded");
    }
    let path = FILE_COUNT.load(Ordering::Relaxed);
    FILE_COUNT.fetch_add(1, Ordering::SeqCst);
    let mut output = File::create(path.to_string()).unwrap();
    write!(output, "{}", bytes).unwrap();
    let mut url = String::from("http://localhost:3000/");
    url.push_str(&path.to_string());
    return url;
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/", get(|| async { "Paste something in pastebin! use curl -X POST http://localhost:3000/submit -d 'this is some data'" }))
        .route("/{id}", get(handle))
        .route("/submit", post(submit_handle));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();
    axum::serve(listener, app).await.unwrap();
}

[–] kionite231 2 points 1 month ago

Yeah, I just had to pass numbers by value instead of reference.

[–] kionite231 6 points 1 month ago

Thank you for the suggestion, I will update the code locally :)

[–] kionite231 3 points 1 month ago (1 children)

This is very unneccessary for a simple pastebin clone.

It might be but as I said I am very new to Rust and don't know how to do some stuff in Rust. someone on IRC said I should use Arc to get around this issue but I didn't know much about Arc so I went with unsafe.

[–] kionite231 6 points 1 month ago (1 children)

I use tilde.club to host my website and gemini capsule. It's awersome I don't have to think about managing the server since it's done by the staff of tilde.club

[–] kionite231 5 points 1 month ago (2 children)

It's because I am changing the value of a static variable, there could be more than one thread trying to change the value of FILE_COUNT which could lead to race condition.

[–] kionite231 5 points 2 months ago (5 children)

Can you share which software you are talking about?

[–] kionite231 2 points 2 months ago

I’m going to assume that Python keeps multiple versions of the array in memory

yeah you are right, I am passing the vector of numbers by reference, that's why my solution doesn't work.

[–] kionite231 1 points 2 months ago

Thanks I will try to run the python version and see if it works. I think the problem is that I am passing list of numbers by reference and in python it's passed by value, this is the reason the python version works while mine doesn't.

[–] kionite231 1 points 2 months ago

wow, that's a nice idea. I will try to solve this problem with this method if mine doesn't work.

[–] kionite231 2 points 2 months ago (4 children)

last input isn't working, for the last input answer should be true but I am getting false. here is the output:

rustc t1.rs && ./t1
190
[10, 19]
> 190 true
it's true
3267
[81, 40, 27]
> 87480 false
> 3267 true
it's true
83
[17, 5]
> 85 false
> 22 false
156
[15, 6]
> 90 false
> 21 false
7290
[6, 8, 6, 15]
> 4320 false
> 303 false
> 54 false
> 14 false
161011
[16, 10, 13]
> 2080 false
> 173 false
> 26 false
192
[17, 8, 14]
> 1904 false
> 150 false
> 25 false
21037
[9, 7, 18, 13]
> 14742 false
> 1147 false
> 81 false
> 16 false
292
[11, 6, 16, 20]
> 21120 false
> 1076 false
> 82 false
> 17 false

 

hello,

I want to learn and deep dive into BSD systems. I am a Linux user for more than 3 years and now I am curious to learn and use BSD since BSD is similar to Linux and has binary compatibility.

sadly my laptop wifi card isn't supported by any BSD systems. so I can't use it as my daily driver. so where should I go or do to learn more about BSD?

 

hii,

my first anime was naruto and I loved it. then I started watching blich and it was good.

at last I watched one piece and this anime is astoundingly awesome. it's concept of a new world pirates and marines are mind blowing. I liked the store to find one piece. every character has great and emotional back stories. kuma's back was the best out of all of them. I love the words of doflamingo: "I'm just an ordinary man with an ordinary heart. I'll change the world by becoming the Pirate King! No, by becoming someone greater than any Pirate King! Someone who conquers the sea and usurps the very heavens! No matter what you say, or what anyone else says, I am going to become the greatest! That's what I've decided! I don't give a damn about conquering some worthless seas. Men's dreams will never die! Children who have never seen peace and children who have never seen war have different values! Those who stand at the top determine what's wrong and what's right! This very place is neutral ground! Justice will prevail, you say? But of course it will! Whoever wins this war becomes justice!"

there is so much I can add about one piece but I will keep it short.

I would love to hear about your opinions about your favourite anime

83
submitted 8 months ago* (last edited 8 months ago) by kionite231 to c/[email protected]
 

hii,

I am learning English for around 5 years and I still can't comprehend the meaning of "would" and "count" in some context. are they just past form of "will" and "can"?

"would you like coffee" means a person is asking if you liked coffee in past? "I would do it" means I did it in past?

I really don't understand since my language doesn't have anything like those words.

Edit: Thank you for answering my naive question :)

 

Hii,

I am new to database thing so I am trying to wrap my head around it.

  1. many2one: so in this relationship you will have more than one record in one table which matches to only one record in another table. something like A <-- B. where (<--) is foreign key relationship. so B will have a column which will be mapped to more than one record of A.

  2. one2many: same as many2one but instead now the foreign key constrain will look something like A --> B.

  3. many2many: this one is interesting because this relationship doesn't make use of foreign key directly. to have this relationship between A and B you have to make a third database something like AB_rel. AB_rel will hold values of primary key of A and also primary key of B. so that way we can map those two using AB_rel table.

tell me if I got something wrong :) give me some suggestion <3

 

hi,

I have been suffering from anxiety and was thinking about past bad things that happened to me and by me. I can't forgive myself for what I did and neither I can forgive who did bad things to me. also I keep thinking about stuff that is out of my control and power.

so I came up with a solution whenever that happens I also think myself as powerless and can't do anything. it makes me accepts things as they are. plus it makes me expect help from others.

 

hello,

I have recently bought a VPS from Linode and I thinking about using it to host some stuff but I don't know what I want to host and what I can host. I don't have a domain name so the traffic is http only :(. but I haven't hosted anything sensitive so I think it's fine.

what have you hosted on your VPS or physical server?

 

hi,

My company gave me a laptop for work and after some time they will give it to another person when I quit the company so I want to make sure I don't leave any of my data behind so which command should I run to delete my home directory to delete all the data?

is it just rm -rf /home/myname

68
submitted 9 months ago* (last edited 9 months ago) by kionite231 to c/[email protected]
 

Hello,

I was feeling like I am mentally sick since 3-4 year. I often do unhinged things that doesn't make any sense. I constantly imagine things that's not the reality however I know it's not reality but I still smile thinking about those imaginary stuff and also I get depressed thinking the imaginary stuff. I don't hear any voice but I keep talking to people in my head. It's difficult for me to sympathize to other people. I can't even make any social connection because my brain will make some negative assumption toward that person who is actually a nice person.

I went to psychiatrist and he said I have schizophrenia and told that I need immediate treatment. he gave me 9 injections and medicines. I feel like I am scammed. I don't know I feel the same before and after the treatment. It also cost a lot of money around 1000$. I am so frustrated and don't know what to do.

any suggestions would be appreciated.

Edit: no, I didn't take the treatment from the first psychiatrist. I went to other psychiatrist and he came to the same conclusion. I was so scared atm that I didn't think much about it and went for the treatment.

 

I usually spend 1-2 hour on Lemmy since I am not a good reader(and also non native) so it takes some time to read all the content I like on Lemmy.

I was inactive for some time because I was busy with my life but I am back and now I try to post and comment more frequently!

 

I personally use libera.chat because it's the most active IRC server out there. is there any other active servers? I found this website to search for different IRC servers https://netsplit.de/networks/

view more: ‹ prev next ›