Skip to content

Commit

Permalink
refactor: fix bugs and remove unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
null8626 committed Dec 16, 2023
1 parent 2b8025c commit 2f7b418
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 681 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cfg-if = "1"
reqwest = { version = "0.11", features = ["json"], optional = true }
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["rt", "sync", "time"], optional = true }
urlencoding = { version = "2", optional = true }

chrono = { version = "0.4", default-features = false, optional = true, features = ["serde"] }
serde_json = { version = "1", optional = true }
Expand All @@ -33,7 +34,7 @@ rustc-args = ["--cfg", "docsrs"]

[features]
default = ["api"]
api = ["chrono", "reqwest", "serde_json"]
api = ["chrono", "reqwest", "serde_json", "urlencoding"]
autoposter = ["api", "tokio"]

webhook = []
Expand Down
19 changes: 10 additions & 9 deletions src/autoposter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{client::InnerClient, Stats};
use core::{mem::MaybeUninit, ops::Deref, time::Duration};
use core::{ops::Deref, time::Duration};
use std::sync::Arc;
use tokio::{
sync::Mutex,
Expand All @@ -18,7 +18,7 @@ pub struct AutoposterHandle {
}

impl AutoposterHandle {
/// Feeds new bot stats to this autoposter handle. The [autoposter itself][Autoposter] will automatically post it to the [Top.gg](https://top.gg) servers once appropiate.
/// Feeds new bot stats to this autoposter handle. The [autoposter itself][Autoposter] will automatically post it to [Top.gg](https://top.gg) servers once appropiate.
///
/// # Examples
///
Expand All @@ -36,7 +36,7 @@ impl AutoposterHandle {
///
/// // ... then in some on ready/new guild event ...
/// let server_count = 12345;
/// let stats = Stats::count_based(server_count, None);
/// let stats = Stats::from(server_count);
/// autoposter.feed(stats).await;
/// ```
///
Expand All @@ -54,22 +54,22 @@ impl AutoposterHandle {
///
/// let server_count = 12345;
/// autoposter
/// .feed(Stats::count_based(server_count, None))
/// .feed(Stats::from(server_count))
/// .await;
///
/// // this handle can be cloned and tossed around threads!
/// let new_handle = autoposter.handle();
///
/// // do the same thing...
/// new_handle
/// .feed(Stats::count_based(server_count, None))
/// .feed(Stats::from(server_count))
/// .await;
///
/// let another_handle = new_handle.clone();
///
/// // do the same thing...
/// another_handle
/// .feed(Stats::count_based(server_count, None))
/// .feed(Stats::from(server_count))
/// .await;
/// ```
pub async fn feed(&self, new_stats: Stats) {
Expand All @@ -90,7 +90,9 @@ impl Clone for AutoposterHandle {
}
}

/// A struct that lets you automate the process of posting bot statistics to the [Top.gg API](https://docs.top.gg) in intervals.
/// A struct that lets you automate the process of posting bot statistics to [Top.gg](https://top.gg) in intervals.
///
/// **NOTE:** This struct owns the thread handle that executes the automatic posting. The autoposter thread will stop once it's dropped.
#[must_use]
pub struct Autoposter {
thread: JoinHandle<()>,
Expand All @@ -100,11 +102,10 @@ pub struct Autoposter {
impl Autoposter {
#[allow(invalid_value, clippy::uninit_assumed_init)]
pub(crate) fn new(client: Arc<InnerClient>, interval: Duration) -> Self {
// SAFETY: post_stats will be called ONLY when the ready flag is set to true.
let handle = AutoposterHandle {
data: Arc::new(Mutex::new(PendingData {
ready: false,
stats: unsafe { MaybeUninit::uninit().assume_init() },
stats: Stats::count_based(0, None),
})),
};

Expand Down
Loading

0 comments on commit 2f7b418

Please sign in to comment.