Checkpoint
This commit is contained in:
parent
46451f2a8b
commit
6d4dd0e7a4
11 changed files with 151 additions and 9 deletions
63
crates/util/src/arc_cow.rs
Normal file
63
crates/util/src/arc_cow.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
#[derive(PartialEq, Eq, Hash)]
|
||||
pub enum ArcCow<'a, T: ?Sized> {
|
||||
Borrowed(&'a T),
|
||||
Owned(Arc<T>),
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized> Clone for ArcCow<'a, T> {
|
||||
fn clone(&self) -> Self {
|
||||
match self {
|
||||
Self::Borrowed(borrowed) => Self::Borrowed(borrowed),
|
||||
Self::Owned(owned) => Self::Owned(owned.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized> From<&'a T> for ArcCow<'a, T> {
|
||||
fn from(s: &'a T) -> Self {
|
||||
Self::Borrowed(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<Arc<T>> for ArcCow<'_, T> {
|
||||
fn from(s: Arc<T>) -> Self {
|
||||
Self::Owned(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for ArcCow<'_, str> {
|
||||
fn from(value: String) -> Self {
|
||||
Self::Owned(value.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized + ToOwned> std::borrow::Borrow<T> for ArcCow<'a, T> {
|
||||
fn borrow(&self) -> &T {
|
||||
match self {
|
||||
ArcCow::Borrowed(borrowed) => borrowed,
|
||||
ArcCow::Owned(owned) => owned.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> std::ops::Deref for ArcCow<'_, T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
match self {
|
||||
ArcCow::Borrowed(s) => s,
|
||||
ArcCow::Owned(s) => s.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> AsRef<T> for ArcCow<'_, T> {
|
||||
fn as_ref(&self) -> &T {
|
||||
match self {
|
||||
ArcCow::Borrowed(borrowed) => borrowed,
|
||||
ArcCow::Owned(owned) => owned.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,7 +2,7 @@ pub use anyhow::{anyhow, Result};
|
|||
use futures::future::BoxFuture;
|
||||
use isahc::config::{Configurable, RedirectPolicy};
|
||||
pub use isahc::{
|
||||
http::{Method, Uri},
|
||||
http::{Method, StatusCode, Uri},
|
||||
Error,
|
||||
};
|
||||
pub use isahc::{AsyncBody, Request, Response};
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
pub mod arc_cow;
|
||||
pub mod channel;
|
||||
pub mod fs;
|
||||
pub mod github;
|
||||
|
@ -246,9 +247,16 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
struct Defer<F: FnOnce()>(Option<F>);
|
||||
pub struct Deferred<F: FnOnce()>(Option<F>);
|
||||
|
||||
impl<F: FnOnce()> Drop for Defer<F> {
|
||||
impl<F: FnOnce()> Deferred<F> {
|
||||
/// Drop without running the deferred function.
|
||||
pub fn cancel(mut self) {
|
||||
self.0.take();
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FnOnce()> Drop for Deferred<F> {
|
||||
fn drop(&mut self) {
|
||||
if let Some(f) = self.0.take() {
|
||||
f()
|
||||
|
@ -256,8 +264,9 @@ impl<F: FnOnce()> Drop for Defer<F> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn defer<F: FnOnce()>(f: F) -> impl Drop {
|
||||
Defer(Some(f))
|
||||
/// Run the given function when the returned value is dropped (unless it's cancelled).
|
||||
pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
|
||||
Deferred(Some(f))
|
||||
}
|
||||
|
||||
pub struct RandomCharIter<T: Rng>(T);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue