1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Implementation of [`Builder`].

use std::future::Future;
use std::io::{self, Error, ErrorKind};

use super::{r#impl, JoinHandle, Scope, ScopedJoinHandle};
#[cfg(feature = "message")]
use crate::web::message::MessageSend;

/// See [`std::thread::Builder`].
#[derive(Debug)]
#[must_use = "must eventually spawn the thread"]
pub struct Builder(r#impl::Builder);

impl Builder {
	/// See [`std::thread::Builder::new()`].
	#[allow(clippy::new_without_default)]
	pub fn new() -> Self {
		Self(r#impl::Builder::new())
	}

	/// See [`std::thread::Builder::name()`].
	pub fn name(self, name: String) -> Self {
		Self(self.0.name(name))
	}

	/// See [`std::thread::Builder::spawn()`].
	///
	/// # Errors
	///
	/// If the main thread does not support spawning threads, see
	/// [`web::has_spawn_support()`](crate::web::has_spawn_support).
	#[allow(clippy::type_repetition_in_bounds)]
	pub fn spawn<F, T>(self, #[allow(clippy::min_ident_chars)] f: F) -> io::Result<JoinHandle<T>>
	where
		F: FnOnce() -> T,
		F: Send + 'static,
		T: Send + 'static,
	{
		if super::has_spawn_support() {
			self.0.spawn(f).map(JoinHandle::new)
		} else {
			Err(Error::new(
				ErrorKind::Unsupported,
				"operation not supported on this platform without the atomics target feature and \
				 cross-origin isolation",
			))
		}
	}

	/// Implementation for
	/// [`BuilderExt::spawn_async()`](crate::web::BuilderExt::spawn_async).
	pub(crate) fn spawn_async_internal<F1, F2, T>(self, task: F1) -> io::Result<JoinHandle<T>>
	where
		F1: 'static + FnOnce() -> F2 + Send,
		F2: 'static + Future<Output = T>,
		T: 'static + Send,
	{
		if super::has_spawn_support() {
			self.0.spawn_async_internal(task).map(JoinHandle::new)
		} else {
			Err(Error::new(
				ErrorKind::Unsupported,
				"operation not supported on this platform without the atomics target feature and \
				 cross-origin isolation",
			))
		}
	}

	/// Implementation for
	/// [`BuilderExt::spawn_with_message()`](crate::web::BuilderExt::spawn_with_message).
	#[cfg(feature = "message")]
	pub(crate) fn spawn_with_message_internal<F1, F2, T, M>(
		self,
		task: F1,
		message: M,
	) -> io::Result<JoinHandle<T>>
	where
		F1: 'static + FnOnce(M) -> F2 + Send,
		F2: 'static + Future<Output = T>,
		T: 'static + Send,
		M: 'static + MessageSend,
	{
		if super::has_spawn_support() {
			self.0
				.spawn_with_message_internal(task, message)
				.map(JoinHandle::new)
		} else {
			Err(Error::new(
				ErrorKind::Unsupported,
				"operation not supported on this platform without the atomics target feature and \
				 cross-origin isolation",
			))
		}
	}

	/// See [`std::thread::Builder::spawn_scoped()`].
	///
	/// # Errors
	///
	/// If the main thread does not support spawning threads, see
	/// [`web::has_spawn_support()`](crate::web::has_spawn_support).
	pub fn spawn_scoped<'scope, #[allow(single_use_lifetimes)] 'env, F, T>(
		self,
		scope: &'scope Scope<'scope, 'env>,
		#[allow(clippy::min_ident_chars)] f: F,
	) -> io::Result<ScopedJoinHandle<'scope, T>>
	where
		F: FnOnce() -> T + Send + 'scope,
		T: Send + 'scope,
	{
		if super::has_spawn_support() {
			self.0.spawn_scoped(&scope.this, f)
		} else {
			Err(Error::new(
				ErrorKind::Unsupported,
				"operation not supported on this platform without the atomics target feature and \
				 cross-origin isolation",
			))
		}
	}

	/// Implementation for
	/// [`BuilderExt::spawn_scoped_async()`](crate::web::BuilderExt::spawn_scoped_async).
	pub(crate) fn spawn_scoped_async_internal<'scope, F1, F2, T>(
		self,
		scope: &'scope Scope<'scope, '_>,
		task: F1,
	) -> io::Result<ScopedJoinHandle<'scope, T>>
	where
		F1: 'scope + FnOnce() -> F2 + Send,
		F2: 'scope + Future<Output = T>,
		T: 'scope + Send,
	{
		if super::has_spawn_support() {
			self.0.spawn_scoped_async_internal(&scope.this, task)
		} else {
			Err(Error::new(
				ErrorKind::Unsupported,
				"operation not supported on this platform without the atomics target feature and \
				 cross-origin isolation",
			))
		}
	}

	/// Implementation for
	/// [`BuilderExt::spawn_scoped_with_message()`](crate::web::BuilderExt::spawn_scoped_with_message).
	#[cfg(feature = "message")]
	pub(crate) fn spawn_scoped_with_message_internal<'scope, F1, F2, T, M>(
		self,
		scope: &'scope Scope<'scope, '_>,
		task: F1,
		message: M,
	) -> io::Result<ScopedJoinHandle<'scope, T>>
	where
		F1: 'scope + FnOnce(M) -> F2 + Send,
		F2: 'scope + Future<Output = T>,
		T: 'scope + Send,
		M: 'scope + MessageSend,
	{
		if super::has_spawn_support() {
			self.0
				.spawn_scoped_with_message_internal(&scope.this, task, message)
		} else {
			Err(Error::new(
				ErrorKind::Unsupported,
				"operation not supported on this platform without the atomics target feature and \
				 cross-origin isolation",
			))
		}
	}

	/// See [`std::thread::Builder::stack_size()`].
	///
	/// # Notes
	///
	/// Stack size will be round up to the nearest multiple of the WebAssembly
	/// page size, which is 64 Ki.
	pub fn stack_size(self, size: usize) -> Self {
		Self(self.0.stack_size(size))
	}
}